简体   繁体   中英

how to compare node with each other in xml file?

I am new in working with xml. My requirement is to compare each node with other node in same sml file.

在此处输入图片说明

for example book is root tag sub tag is author,title,genre,price,publish_date this structure compare with other node how it possible in java. and give me some links and if possible code also.

You can convert every element into java object POJO. Then by overriding the equals() method. Now you will have list of objects. Now iterate over the list and compare every object with every other object.

you could use a simple DOM Parser to read from your XML file. Read all your elements and save them to objects(books), then you could compare their values as you want. Here is a sample how to read your xml file:

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXMLFile {

    public static void main(String argv[]) {

        try {
            File fXmlFile = new File("nodes.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("catalog");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                System.out.println("Current Element :" + nNode.getNodeName());

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    System.out.println("Author : " + eElement.getElementsByTagName("author").item(0).getTextContent());
                    System.out.println("Title : " + eElement.getElementsByTagName("title").item(0).getTextContent());
                    System.out.println("Genre : " + eElement.getElementsByTagName("genre").item(0).getTextContent());
                    System.out.println("Price : " + eElement.getElementsByTagName("price").item(0).getTextContent());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I tested it with this file: nodes.xml

<?xml version="1.0"?>
<catalog>
    <book id="1">
        <author>Author1</author>
        <title>Title1</title>
        <genre>Genre1</genre>
        <price>1</price>
    </book>
    <book id="2">
        <author>Author2</author>
        <title>Title2</title>
        <genre>Genre2</genre>
        <price>2</price>
    </book>
</catalog>

This is the output for the first element:

Root element :catalog
Current Element :catalog
Author : Author1
Title : Title1
Genre : Genre1
Price : 1

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