如何使用DOM解析嵌套元素

时间:2015-07-09 19:21:49

标签: java xml parsing dom

我正在开发一个应用程序,我想使用DOM解析包含许多嵌套元素的XML文件。以下代码段是我正在处理的XML文件的类型。

<?xml version="1.0"?>
<audobon>
    <bird id="1">
        <title>eagle</title>
        <link id="1">wikipedia.org/eagle</link>
        <description>Large bird of prey</description>

    </bird>
    <bird id="2">
        <title>Duck</title>
        <link id="1">wikipedia.org/wood_duck</link>
        <link id="2">wikipedia.org/mallard_duck</link>
        <description>Aquatic, omnivorous bird.</description>

    </bird>
    <bird id="3">
        <title>Crane</title>
        <link id="1">wikipedia.org/crane</link>     
        <description>Aquatic, carnivorous bird</description>
    </bird>

    <bird id="4">
        <title>pigeon</title>
        <link id="1">wikipedia.org/common_pigeon</link>
        <link id="2">wikipedia.org/passenger_pigeon</link>
        <link id="3">wikipedia.org/homing_pigeon</link>
        <description>Domesticated or wild bird</description>

    </bird>

</audobon>

因此,在这个例子中,我想浏览每一个“bird”元素,并删除“链接”元素,其数量是可变的。

这是我目前正在使用的代码。

public static void main(String[] args) {
    try {
        File fXmlFile = new File("C:/Users/I844763/Documents/AudobonXML.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("bird");
        ParsedDataLength = nList.getLength();
        NodeList LinkList = null;

        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                System.out.println("Bird Id: " + eElement.getAttribute("id"));
                System.out.println("Description: " + eElement.getElementsByTagName("description").item(0).getTextContent());
                System.out.println("Title : " + eElement.getElementsByTagName("title").item(0).getTextContent());

                //need method for setting value of i here to number of links in individual Bird section

                for (int i = 0; temp < nList.getLength(); i++) {
                    System.out.println("    Link : " + eElement.getElementsByTagName("link").item(i).getTextContent());
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

所以,基本上我需要一种方法来确定如何在第二个循环中确定i的值。我想在XML文件中添加一个包含每篇文章中包含的链接数量的附加元素,但我想要一种更灵活的方式来实现它。感谢。

1 个答案:

答案 0 :(得分:0)

也许我不理解,但这不是你想要的吗?

//need method for setting value of i here to number of links in individual Bird section

NodeList linkNodes = eElement.getElementsByTagName("link");

for (int i = 0; i < linkNodes.getLength(); i++) {
    System.out.println("    Link : " + linkNodes.item(i).getTextContent());
}

使用该添加运行代码会产生以下输出:

Root element :audobon

Current Element :bird
Bird Id: 1
Description: Large bird of prey
Title : eagle
    Link : wikipedia.org/eagle

Current Element :bird
Bird Id: 2
Description: Aquatic, omnivorous bird.
Title : Duck
    Link : wikipedia.org/wood_duck
    Link : wikipedia.org/mallard_duck

Current Element :bird
Bird Id: 3
Description: Aquatic, carnivorous bird
Title : Crane
    Link : wikipedia.org/crane

Current Element :bird
Bird Id: 4
Description: Domesticated or wild bird
Title : pigeon
    Link : wikipedia.org/common_pigeon
    Link : wikipedia.org/passenger_pigeon
    Link : wikipedia.org/homing_pigeon