我正在使用parse xml
尝试jsoup
数据。我将每个数据都放入array list
。但是,当我得到data
时,每个data
都会进入同一行。
例如,我得到的数据是
RESULT
ITEM
R1
B DESC =""
B1
C DESC =""
CR1
C1
CR2
C1
CR3
C1
我的java代码是
List<Object> list = new ArrayList<>();
Elements elements = document.select("ITEM");
for (Element element : elements) {
medicationBasic = new MedicationBasic();
medicationBasic.setProductName(element.select("B1").text());
medicationOrigin = new MedicationOrigin();
medicationOrigin.setMedOrigin(element.select("C1").text());
list.add(medicationBasic);
list.add(medicationOrigin);
response = new ResponseList(Result.SUCCESS, (ArrayList) list);
我得到的是:
{
productName:"B1",
medOrigin:"C1(from CR1) C1(from CR2) C1(from CR3)"
}
预期结果:
{
productName:"B1",
medOrigin:"C1(from CR1)"
}
{
productName:"B1",
medOrigin:"C1(from CR2)"
}
{
productName:"B1",
medOrigin:"C1(from CR3)"
}
我的代码出了什么问题?请帮帮我...
答案 0 :(得分:0)
element.select("C1")
选择ITEM内的所有C1元素(返回Elements
),text()
返回combined text:
public String text()获取所有匹配的组合文本 元件。
<强>修正强> 迭代C1元素并添加键值对。
示例代码
String xmlString = "<RESULT>\r\n" +
" <ITEM>\r\n" +
" <R1>\r\n" +
" <B DESC =\"\">\r\n" +
" <B1>B1<B1>\r\n" +
" </B>\r\n" +
" <C DESC =\"\">\r\n" +
" <CR1>\r\n" +
" <C1>C1(from CR1)</C1>\r\n" +
" </CR1>\r\n" +
" <CR2>\r\n" +
" <C1>C1(from CR2)</C1>\r\n" +
" </CR2>\r\n" +
" <CR3>\r\n" +
" <C1>C1(from CR3)</C1>\r\n" +
" </CR3>\r\n" +
" </C>\r\n" +
" </R1>\r\n" +
" </ITEM>\r\n" +
"</RESULT>";
Document document = Jsoup.parse(xmlString);
List<SimpleEntry<String, String>> list = new ArrayList<>();
Elements elements = document.select("ITEM");
for (Element element : elements) {
String medicationBasic = element.select("B1").text();
element.select("C1").forEach(c1Element -> {
String medicationOrigin = c1Element.text();
list.add(new SimpleEntry<String, String>(medicationBasic, medicationOrigin));
});
}
System.out.println(list);
<强>输出强>
[B1 =C1(from CR1), B1 =C1(from CR2), B1 =C1(from CR3)]
答案 1 :(得分:0)
为了将来的参考,我通过以下方式解决了我的问题:
List<Object> list = new ArrayList<>();
Elements bElement = document.select("B DESC");
Elements cElement = document.select("C DESC>*");
for (Element bElement : elements) {
medicationBasic = new MedicationBasic();
medicationBasic.setProductName(element.select("B1").text());
list.add(medicationBasic);
}
for (Element cElement : elements) {
medicationOrigin = new MedicationOrigin();
medicationOrigin.setMedOrigin(element.select("C1").text());
list.add(medicationOrigin);
}
response = new ResponseList(Result.SUCCESS, (ArrayList) list);
我得到了我期望的结果