简体   繁体   中英

Parsing an XML file in JDOM for child-child nodes

I have an XML file in the following manner:

<head>
                <username>bhnsub</username>
                <error>0</error>
                <account_id>633</account_id>

        <info>
        <mac>address_goes_here<mac>
        <mac>address_goes_here</mac>
        <mac>address_goes_here</mac>
        <mac>address_goes_here</mac>
        <mac>address_goes_here<mac>
    </info>
</head>

I need to parse it using Java DOM parser and get the corresponding values. I need to put the values under info in a list.

    SAXBuilder builder = new SAXBuilder();
   Document document = (Document) builder.build(new StringReader(content));
            Element rootNode = document.getRootElement();
            if (rootNode.getName().equals("head")) {
                String username = rootNode.getChildText("username");
                String error= rootNode.getChildText("error");
                String account= rootNode.getChildText("account_id");
                Element info= rootNode.getChildren("info");
                        List mac=info.getChildren("mac");

I am not getting how to proceed further and use the list.

This works, using stuff from javax.xml.parsers and org.w3c.dom.

List<String> macvals = new ArrayList<>();
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = db.parse(new File( "head.xml" ) );
Element rootNode = document.getDocumentElement();
if (rootNode.getTagName().equals("head")) {
    NodeList infos = rootNode.getElementsByTagName("info");
    if( infos.getLength() > 0 ){
    Element info = (Element)infos.item(0);
    NodeList macs = info.getElementsByTagName("mac");
    for( int i = 0; i < macs.getLength(); ++i ){
        macvals.add( macs.item( i ).getTextContent() );
    }
    }
}
System.out.println( macvals );

First up, please ensure you are using JDOM 2.0.6 (or later if you are reading this in the future). JDOM 2.x has been out for 5 years or so, and is better because it supports Java generics, it has performance improvements, and it has better XPath support too, if you need it.

Still, your code would be "easily" written as:

SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new StringReader(content));
Element rootNode = document.getRootElement();
if ("head".equals(rootNode.getName())) {
    String username = rootNode.getChildText("username");
    String error= rootNode.getChildText("error");
    String account= rootNode.getChildText("account_id");
    List<String> macs = new ArrayList<>();
    for (Element info : rootNode.getChildren("info")) {
        for (Element mac : info.getChildren("mac")) {
            macs.add(mac.getValue());
        }
    }
}

Note that I have put 2 loops in there. Your code has a bug, because it calls:

 Element info = rootNode.getChildren("info"); 

but getChildren(...) returns a List, so that can't work. In my code above I iterate through the list instead. If there is only one "info" element, then the list will have only one member.

Also note that, in JDOM 2.x, the getChildren(..) method returns a list of Element: List<Element> so there is no need to cast the results to Element .

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