0

我的 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

请帮忙

4

1 回答 1

1

查看 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
于 2013-10-15T10:50:38.247 回答