search
HomeJavaGet data from XML document using DOM in Java

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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools