简体   繁体   中英

Not able to parse the xml using jsoup

My xml String is like this :

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

How can i get the education list means school names . My requirement is like this , Get the list of education details from the first data tag start and data tag end , then 2nd data tag start ,data tag end and 3rd so on. so i have tried like this , but not able to fetch the data.

    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);
                    }

            }
                           }

Expected output:In 1st Iteration i want to get : SSSSSSSSSS , SSS , University ,Hellio

Please Help

Look at your structure in the XML.

If you want the name of the school, then it's structured as follows:

<education>
       <school>
             <name>

To select that, simply use

   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());
    }

This will output

run:
SSSSSSSSSS
SSS , University
Hellio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM