如何与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结构?
答案 0 :(得分: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();
}
}
}
答案 1 :(得分:1)
您可以计算XML结构的哈希值并比较两个哈希值。 以下是StAX的简单示例,示例仅关注start和end元素,您可能希望添加其他类型。它将哈希值打印到控制台而不是比较。
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();
}
}