目前,我有以下XML
这样的内容:
<item>
<title> this is title 1 </title>
<description> description 1 </description>
<pubDate> date 1 </pubDate>
</item>
<item>
<title> this is title 2 </title>
<description> description 2 </description>
<pubDate> date 2 </pubDate>
</item>
我使用的是jsoup
,但我得到的结果是:
this is title 1
this is title 2
description 1
description 2
date 1
date 2
我想要的实际结果:
this is title 1
description 1
date 1
this is title 2
description 2
date 2
我还是android的初学者。我想使用Jsoup
实现此目的。一个简单的示例代码会很有帮助。谢谢。
答案 0 :(得分:1)
以下是打印item
元素的所有子元素的代码段:
public class Test {
public static void main(String[] args) {
String xml =
"<item>\r\n" +
" <title> this is title 1 </title>\r\n" +
" <description> description 1 </description>\r\n" +
" <pubDate> date 1 </pubDate>\r\n" +
"</item>\r\n" +
"\r\n" +
"<item>\r\n" +
" <title> this is title 2 </title>\r\n" +
" <description> description 2 </description>\r\n" +
" <pubDate> date 2 </pubDate>\r\n" +
"</item>";
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
for (Element item : doc.select("item")) {
Elements children = item.children();
for (Element child : children) {
System.out.println(child.text());
}
}
}
}
这是输出:
这是标题1
描述1
日期1
这是标题2
说明2
日期2
答案 1 :(得分:0)
试试这个:
public class Test {
public static void main(String[] args) {
String html = "<?xml version=\"1.0\" encoding=\"UTF-8\">" +
"<item>\n" +
" <title> this is title 1 </title>\n" +
" <description> description 1 </description>\n" +
" <pubDate> date 1 </pubDate>\n" +
"</item>\n" +
"\n" +
"<item>\n" +
" <title> this is title 2 </title>\n" +
" <description> description 2 </description>\n" +
" <pubDate> date 2 </pubDate>\n" +
"</item></xml>";
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
for (Element e : doc.select("item")) {
System.out.println(e.text());
}
}