java xml从子节点获取属性值

时间:2014-08-27 13:59:11

标签: java xml

我有一个xml文件,我试图获得标题和第一作者。

这是我正在使用的数据:

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

我写的代码

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

我想要获得的输出是:

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

获得标题不是问题,但获得第一作者是。如果我尝试获取NodeValue,我只得到&#34; null&#34;,如果我尝试获取TextConent,我只得到空行。我真的希望有人可以帮助我。提前谢谢!

2 个答案:

答案 0 :(得分:0)

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

我省略了可能需要的节点测试和其他健全性检查。

请查看JAXB。

答案 1 :(得分:0)

试试这个。如果您愿意,可以优化它:)

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