1

我如何与 XML 结构进行比较,忽略标签内容?

例如,以下响应:

<note>
   <to>Adam</to>
   <from>Eve</from>
</note>

和:

<note>
   <to>John</to>
   <from>Joan</from>
</note>

具有相同的 XML 结构,但是:

<note>
   <from>Joan</from>
   <to>John</to>
</note>

与第一个示例不同,因为标签的顺序不同。

如何比较 Java 中的 XPath 结构?

4

2 回答 2

1

您可以使用 JAVA DOM Parse 或 SAX 解析器

下面是访问元素的示例,之后您需要进行正常比较

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

  public static void main(String argv[]) {

    try {

    File fXmlFile = new File("/Input.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("staff");

    System.out.println("----------------------------");

    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("Note: " + eElement.getAttribute("note"));
            System.out.println("From: " + eElement.getElementsByTagName("from").item(0).getTextContent());
            System.out.println("To : " + eElement.getElementsByTagName("to").item(0).getTextContent());

        }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
  }

}
于 2013-04-28T09:27:46.767 回答
1

您可以计算 XML 结构的哈希值并比较两个哈希值。StAX 的简单示例如下,示例仅关心开始和结束元素,您可能需要添加其他类型。它将哈希值打印到控制台而不是比较。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.security.MessageDigest;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;

public class ComputeXMLStructureHash {

    public static void main(String[] args)
    {
        try {
            FileInputStream in1 = new FileInputStream(new File("file1.xml"));
            FileInputStream in2 = new FileInputStream(new File("file2.xml"));
            FileInputStream in3 = new FileInputStream(new File("file3.xml"));

            System.out.println(digest(in1));
            System.out.println(digest(in2));
            System.out.println(digest(in3));                        

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static String digest(InputStream in) {
        MessageDigest messageDigest = null;

        // StAX for XML parsing
        XMLInputFactory inputFactory = XMLInputFactory.newFactory();

        try {
            messageDigest = MessageDigest.getInstance("MD5");
            XMLEventReader eventReader = inputFactory.createXMLEventReader(in);

            // Iterate over the XML elements and update hash
            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();

                if (event.isStartElement()) {
                    messageDigest.update(event.asStartElement().getName().toString().getBytes());                   
                } else if (event.isEndElement()) {
                    messageDigest.update(event.asEndElement().getName().toString().getBytes());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        StringBuffer result = new StringBuffer();
        byte[] digest = messageDigest.digest();
        for (byte b : digest) 
            result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));

        return result.toString(); 
    }
}
于 2013-04-28T10:34:20.940 回答