[英]Not able to parse the xml using jsoup
我的xml字符串是這樣的:
<facebookfriends>
<data>
<id> 501334283</id>
<location>
<id>46464 </id>
<name>abc </name>
</location>
<name> Name Something</name>
<education>
<school>
<id> 45454 </id>
<name> SSSSSSSSSS</name>
</school>
<year>
<id> 45353</id>
<name> 2001</name>
</year>
<type>High School </type>
</education>
<education>
<school>
<id>
134960646529265</id>
<name> SSS , University</name>
</school>
<year>
<id> 132393000129123</id>
<name> 2001</name>
</year>
<type>High School </type>
</education>
<education>
<concentration>
<id> 6868</id>
<name> Computer Science and Engineering
</name>
</concentration>
<school>
<id> 86868</id>
<name>
Hellio
</name>
</school>
<year>
<id> 4646</id>
<name> 2008</name>
</year>
<type>College </type>
</education>
</data>
<data>XYZ</data>
</facebookfriends>
我如何獲得教育清單所指的學校名稱。 我的要求是這樣的,從第一個數據標簽開始和數據標簽結束獲取教育詳情列表,然后從第二個數據標簽開始,數據標簽結束和第三個開始。 所以我已經嘗試過這樣,但無法獲取數據。
StringReader reader = new StringReader(xmlbody);//XML body is my xml string
InputSource source = new InputSource(reader);
Document document = dbBuilder.parse(source);
NodeList dataList = document.getElementsByTagName("data");
for(int i=0;i<dataList.getLength();i++) {
Node node = dataList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE) {
Element data = (Element)node;
org.jsoup.nodes.Document xmlDoc = Jsoup.parse(data.getTextContent(), "", Parser.xmlParser());
for (org.jsoup.nodes.Element e : xmlDoc.select("education")) {
System.out.println(e);
}
}
}
預期輸出:在第一次迭代中,我要獲得:SSSSSSSSSS,SSS,大學,Hellio
請幫忙
查看XML中的結構。
如果您想要學校的名稱,則其結構如下:
<education>
<school>
<name>
要選擇它,只需使用
Document doc = Jsoup.parse(xml);
Elements e = doc.select("education school name"); //Tree structure for tags
for (Element el : e) {
System.out.println(el.text());
}
這將輸出
run:
SSSSSSSSSS
SSS , University
Hellio
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.