通过相同的节点比较Java中两个不同的XML文件
我有两个不同的XML文件要比较 假设我想比较这些XML文件的具体内容 例如: 每个XML文件都有一个公共节点,称为:通过相同的节点比较Java中两个不同的XML文件,java,xml,Java,Xml,我有两个不同的XML文件要比较 假设我想比较这些XML文件的具体内容 例如: 每个XML文件都有一个公共节点,称为: <BURAK> <burak1> <burak2>a<burak2> <BURAK> <burak1> <burak2>c<burak2> 我找到了一个有用的代码来创建这个结构,但是这段代码给了我两个文件之间的所有差异。 我想要的是转到特定节点并比
<BURAK>
<burak1>
<burak2>a<burak2>
<BURAK>
<burak1>
<burak2>c<burak2>
我找到了一个有用的代码来创建这个结构,但是这段代码给了我两个文件之间的所有差异。
我想要的是转到特定节点并比较该节点中的特定子节点。
我如何用Java编写这种代码
关于“svasa”的建议,我以这种方式更改了代码:
try
{
String firstValue = null;
String secondValue = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse( new File( "/abc.xml" ) );
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile( "//TOC/STRUCTURE/TOC_NODE/NODE_NAME");
Object exprValue = expr.evaluate( doc, XPathConstants.STRING );
if ( exprValue != null )
{
firstValue = exprValue.toString();
}
Document doc1 = db.parse( new File( "/def.xml" ) );
XPathFactory xPathFactory1 = XPathFactory.newInstance();
XPath xpath1 = xPathFactory1.newXPath();
XPathExpression expr1 = xpath1.compile( "//TOC/cac:STRUCTURE/cac:TOC_NODE/cac:NODE_NAME");
Object exprValue1 = expr1.evaluate( doc1, XPathConstants.STRING );
if ( exprValue1 != null )
{
secondValue = exprValue1.toString();
}
if ( firstValue != null && secondValue != null )
{
System.out.println( firstValue );
System.out.println( secondValue );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
这不起作用,或者我无法让它起作用。如果这是我想要的正确结构。代码在我看来是合乎逻辑的。正如我在评论中提到的,我确实需要第二条路径。结构看起来很相似,但不完全相同。使用。它很容易编码、编译xpath表达式并使用它从xml中提取所需的数据
代码为:
try
{
String firstValue = null;
String secondValue = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse( new File( "/abc.xml" ) );
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile( "//BURAK/burak1/burak2");
Object exprValue = expr.evaluate( doc, XPathConstants.STRING );
if ( exprValue != null )
{
firstValue = exprValue.toString();
}
doc = db.parse( new File( "/def.xml" ) );
exprValue = expr.evaluate( doc, XPathConstants.STRING );
if ( exprValue != null )
{
secondValue = exprValue.toString();
}
if ( firstValue != null && secondValue != null )
{
System.out.println( firstValue );
System.out.println( secondValue );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
xpath//BURAK/burak1/burak2
将结果指定为XPathConstants.STRING
因为您的两个XML相似,所以相同的xpath表达式可以工作
尝试执行代码,您将获得值a
和c
,然后您可以进行比较。Try
try
{ File inputFile = new File("abc.xml");
DocumentBuilderFactory dbFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
String expression="NODE";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node nNode = nodeList.item(i);
System.out.println("\nCurrent Element :"
+ nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("NODE_NAME: "
+ eElement
.getElementsByTagName("NODE")
.item(0)
.getTextContent());
System.out.println("NODE: "
+ eElement
.getElementsByTagName("NODE")
.item(0)
.getTextContent());
File inputFile1 = new File("def.xml");
DocumentBuilderFactory dbFactory1
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder1;
dBuilder1 = dbFactory1.newDocumentBuilder();
Document doc1 = dBuilder1.parse(inputFile1);
doc1.getDocumentElement().normalize();
XPath xPath1 = XPathFactory.newInstance().newXPath();
String expression1="NODE1";
NodeList nodeList1 = (NodeList) xPath1.compile(expression1).evaluate(doc1, XPathConstants.NODESET);
for (int j = 0; j < nodeList1.getLength(); j++) {
Node nNode1 = nodeList1.item(j);
System.out.println("\nCurrent Element :"
+ nNode1.getNodeName());
if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement1 = (Element) nNode1;
System.out.println("NODE: "
+ eElement1
.getElementsByTagName("NODE_NAME")
.item(0)
.getTextContent());
System.out.println("NODE: "
+ eElement1
.getElementsByTagName("NODE_NAME")
.item(0)
.getTextContent());
}
{File inputFile=新文件(“abc.xml”);
DocumentBuilderFactory数据库工厂
=DocumentBuilderFactory.newInstance();
文档生成器dBuilder;
dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
XPath=XPathFactory.newInstance().newXPath();
字符串表达式=“节点”;
NodeList NodeList=(NodeList)xPath.compile(expression.evaluate(doc,XPathConstants.NODESET);
for(int i=0;i
这是我对这个问题的回答。感谢“svasa”的建议。cod看起来很合理,但不幸的是它们并不完全相同,所以我应该为其他文件定义另一个xpath表达式来匹配它们。如果您使用第二个xml更新问题,我们可以查看是否需要另一个xpath
try
{ File inputFile = new File("abc.xml");
DocumentBuilderFactory dbFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
String expression="NODE";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node nNode = nodeList.item(i);
System.out.println("\nCurrent Element :"
+ nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("NODE_NAME: "
+ eElement
.getElementsByTagName("NODE")
.item(0)
.getTextContent());
System.out.println("NODE: "
+ eElement
.getElementsByTagName("NODE")
.item(0)
.getTextContent());
File inputFile1 = new File("def.xml");
DocumentBuilderFactory dbFactory1
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder1;
dBuilder1 = dbFactory1.newDocumentBuilder();
Document doc1 = dBuilder1.parse(inputFile1);
doc1.getDocumentElement().normalize();
XPath xPath1 = XPathFactory.newInstance().newXPath();
String expression1="NODE1";
NodeList nodeList1 = (NodeList) xPath1.compile(expression1).evaluate(doc1, XPathConstants.NODESET);
for (int j = 0; j < nodeList1.getLength(); j++) {
Node nNode1 = nodeList1.item(j);
System.out.println("\nCurrent Element :"
+ nNode1.getNodeName());
if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement1 = (Element) nNode1;
System.out.println("NODE: "
+ eElement1
.getElementsByTagName("NODE_NAME")
.item(0)
.getTextContent());
System.out.println("NODE: "
+ eElement1
.getElementsByTagName("NODE_NAME")
.item(0)
.getTextContent());
}