Home  >  Article  >  Get data from XML document using DOM in Java

Get data from XML document using DOM in Java

王林
王林forward
2024-02-22 13:01:14382browse

php editor Strawberry will show you how to use DOM to obtain data from XML documents in Java. DOM (Document Object Model) is a standard way of processing XML documents. By manipulating the XML tree structure, we can easily obtain information such as nodes, elements, and attributes. In Java, we can make use of DOM parser to read XML documents and extract the required data. Then, we can traverse the nodes of the XML document and extract the data we need to achieve flexible and efficient data acquisition operations.

Question content

I'm trying to parse an xml file using dom, but I end up outputting only the last entry multiple times (in a loop). Please tell me, is there any problem? (I am a beginner in java)

xml contains approximately the following content:

<channel>
    <title>www.google.com</title>
    <item>
        <title>www.youtube.com</title>
    </item>
    <item>
        <title>play.google.com</title>
    </item>
    ...
</channel>

java code:

Node item = null;
    NodeList channelList = document.getElementsByTagName("channel");
    for (int i = 0; i < channelList.getLength(); i++){

        if (channelList.item(i).getNodeType() == Node.ELEMENT_NODE){
            
            NodeList channelListChild = channelList.item(i).getChildNodes();
            for (int j = 0; j < channelListChild.getLength(); j++){
                if (channelListChild.item(j).getNodeType() == Node.ELEMENT_NODE){

                    switch (channelListChild.item(j).getNodeName()){
                        case "title":{
                            String title = channelListChild.item(j).getTextContent();
                            System.out.println(title);
                            break;
                        }
                        case "item":{
                            item = channelListChild.item(j);
                            break;
                        }
                    }
                }
            }
        }
    }
    if (item == null){
        return;
    }

    String title = null;
    NodeList itemList = item.getChildNodes();

    for (int i = 0; i < itemList.getLength(); i++){
        if (itemList.item(i).getNodeType() == Node.ELEMENT_NODE){

            switch (itemList.item(i).getNodeName()){
                case "title":{
                    title = itemList.item(i).getTextContent();
                    break;
                }
            }
        }
        System.out.println(title);
    }

Workaround

You assign the variable "item" to each item in turn in the loop, so when you exit its value is the last item element encountered .

You are not the only one using java and dom in such applications; it is a popular combination. But it's almost 25 years old, and there are plenty of better options available. Even if it has to be java, there are other apis like jdom2 and xom that are more friendly to use.

Saxon's tree navigation api (inspired by linq in the c# world) looks like this:

XdmNode channel = document.getOutermostElement();
String title = channel.select(child("title")).asString();
System.out.println(title);
channel.select(child("item")).forEach(item -> {
   String subtitle = item.select("title").asString();
   System.out.println(subtitle);
}

This is a fraction of the length of equivalent dom code.

[Disclaimer: saxon is our company's product]

The above is the detailed content of Get data from XML document using DOM in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete