简体   繁体   中英

java xml getting attribute values from subnodes

I got an xml file and im trying to get the title and the first author.

This is the data im working with:

    <citation type="Book" date="1986" name="Book name">

        <title>
            a fun name for a book
        </title>

        <authorList>
            <person name="Person 1"/>
            <person name="Person 2"/>
            <person name="Person 3"/>
        </authorList>

    </citation>

    <citation type="Book" date="1986" name="Another book">

        <title>
            a boring book title
        </title>

        <authorList>
            <person name="Person A"/>
            <person name="Person B"/>
            <person name="Person C"/>
        </authorList>

    </citation>

The code I wrote

NodeList itemsCitation = doc.getElementsByTagName("citation");

for (int i = 0; i < itemsCitation.getLength(); i++) {
Node n = itemsCitation.item(i);

if (n.getNodeType() == Node.ELEMENT_NODE) {

Element e = (Element) n;
NodeList titleNode = e.getChildNodes();

for (int j = 0; j < titleNode.getLength(); j++) {
        Node n2 = titleNode.item(j);

        if (n2.getNodeType() == Node.ELEMENT_NODE && n2.getNodeName().equals("title")) {
                System.out.println(n2.getTextContent());
        }

        if (n2.getNodeType() == Node.ELEMENT_NODE && n2.getNodeName().equals("authorList")) {

                // code i dont have

        }
    }
}

}
} catch (Exception e) {
e.printStackTrace();

The output Im trying to get is:

a fun name for a book
Person 1
a boring book title
Person A

Getting the title isn't the problem but getting the first author is. If I try to get the NodeValue I only get "null", if I try to get the TextConent I only get empty lines. I really hope someone can help me. thanks in advance!

Element root = document.getDocumentElement();
NodeList itemsCitation = root.getElementsByTagName("citation");
for( int it = 0; it < itemsCitation.getLength(); it++ ){
    Node cit = itemsCitation.item( it );
    Element elCit = (Element)cit;
    Node title = elCit.getElementsByTagName("title").item(0);
    Element elTitle = (Element)title;
    System.out.println( "Title: " + elTitle.getTextContent() );
    Node authorList = elCit.getElementsByTagName("authorList").item(0);
    Element elAuthorList =  (Element)authorList;
    NodeList authors = elAuthorList.getElementsByTagName("person");
    for( int ia = 0; ia < authors.getLength(); ia++ ){
        Element elAuthor = (Element)authors.item(ia);
        System.out.print( " " + elAuthor.getAttribute( "name" ) );
    }
    System.out.println();
}

I've omitted the node tests and other sanity checks which might be necessary.

Please, look into JAXB.

Try this. You may optimize it, if you wish :)

        NodeList nodeList = document.getElementsByTagName("citation");

        for (int i = 0; i < nodeList.getLength(); i++) {

            Element citationChildNode = (Element)nodeList.item(i);

            NodeList childNodeList = citationChildNode.getChildNodes();
            if(childNodeList == null){
                continue;
            }

            for(int j = 0; j < childNodeList.getLength(); j++) {
                Node node = childNodeList.item(j);

                if(node.getNodeName().equalsIgnoreCase("title")){
                    Node n = node.getFirstChild();
                    System.out.println(n.getNodeValue());
                }else if(node.getNodeName().equalsIgnoreCase("authorList")){

                    NodeList authorList = node.getChildNodes();
                    Node parent = authorList.item(0);
                    while(true) {
                        parent = parent.getNextSibling();
                        if(parent == null){
                            break;
                        }
                        NamedNodeMap map = parent.getAttributes();
                        if(map == null){
                            continue;
                        }
                        System.out.println(map.getNamedItem("name").getNodeValue());
                        break;
                    }
                }
            }
        }

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