我有2个xml文件a.xml
和b.xml
,b.xml
是a.xml
的修改版本,其中某些节点值已更改。现在我想在我的代码中检查只有特定的节点值可以改变而不是所有的节点。如何在java中进行此检查?
我从用户那里得到这个xml,如果他改变了我不应该允许的其他节点,他只允许更改特定的节点.Below是xml你可以看到
<xt:path>HelloWorld/BusinessServices/Mytest</xt:path>
已经改变,这是有效的情况,因为他也改变了
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">40</xt:value>
从30到40无效。
A.XML
<?xml version="1.0" encoding="UTF-8"?>
<cus:Customizations xmlns:cus="http://www.bea.com/wli/config/customizations" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.bea.com/wli/config/xmltypes">
<cus:customization xsi:type="cus:EnvValueCustomizationType">
<cus:description/>
<cus:envValueAssignments>
<xt:envValueType>Service URI Weight</xt:envValueType>
<xt:location>0</xt:location>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:owner>
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
</cus:envValueAssignments>
<cus:envValueAssignments>
<xt:envValueType>Service URI</xt:envValueType>
<xt:location>0</xt:location>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:owner>
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">jejb:oracle:test</xt:value>
</cus:envValueAssignments>
<cus:envValueAssignments>
<xt:envValueType>Service URI Table</xt:envValueType>
<xt:location xsi:nil="true"/>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:owner>
<xt:value>
<tableElement xmlns="http://www.bea.com/wli/sb/transports">
<URI>jejb:oracle:test</URI>
<weight>0</weight>
</tableElement>
</xt:value>
</cus:envValueAssignments>
<cus:envValueAssignments>
<xt:envValueType>Service Retry Count</xt:envValueType>
<xt:location xsi:nil="true"/>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:owner>
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
</cus:envValueAssignments>
<cus:envValueAssignments>
<xt:envValueType>Service Retry Iteration Interval</xt:envValueType>
<xt:location xsi:nil="true"/>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:owner>
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">30</xt:value>
</cus:envValueAssignments>
</cus:customization>
<cus:customization xsi:type="cus:FindAndReplaceCustomizationType">
<cus:description/>
<cus:query>
<xt:resourceTypes>BusinessService</xt:resourceTypes>
<xt:envValueTypes>Service Retry Iteration Interval</xt:envValueTypes>
<xt:envValueTypes>Service URI Weight</xt:envValueTypes>
<xt:envValueTypes>Service Retry Count</xt:envValueTypes>
<xt:envValueTypes>Service URI</xt:envValueTypes>
<xt:refsToSearch xsi:type="xt:ResourceRefType">
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:refsToSearch>
<xt:includeOnlyModifiedResources>false</xt:includeOnlyModifiedResources>
<xt:searchString>Search String</xt:searchString>
<xt:isCompleteMatch>false</xt:isCompleteMatch>
</cus:query>
<cus:replacement>Replacement String</cus:replacement>
</cus:customization>
<cus:customization xsi:type="cus:ReferenceCustomizationType">
<cus:description/>
<cus:refsToBeConsidered xsi:type="xt:ResourceRefType">
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</cus:refsToBeConsidered>
<cus:externalReferenceMap>
<xt:oldRef>
<xt:type>ForeignJNDIProvider</xt:type>
<xt:path>System/JNDI Providers/oracle</xt:path>
</xt:oldRef>
<xt:newRef>
<xt:type>ForeignJNDIProvider</xt:type>
<xt:path>System/JNDI Providers/oracle</xt:path>
</xt:newRef>
</cus:externalReferenceMap>
<cus:externalReferenceMap>
<xt:oldRef>
<xt:type>Archive</xt:type>
<xt:path>dev/HelloOsbEjbClient</xt:path>
</xt:oldRef>
<xt:newRef>
<xt:type>Archive</xt:type>
<xt:path>dev/HelloOsbEjbClient</xt:path>
</xt:newRef>
</cus:externalReferenceMap>
</cus:customization>
</cus:Customizations>
B.XML
<?xml version="1.0" encoding="UTF-8"?>
<cus:Customizations xmlns:cus="http://www.bea.com/wli/config/customizations" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.bea.com/wli/config/xmltypes">
<cus:customization xsi:type="cus:EnvValueCustomizationType">
<cus:description/>
<cus:envValueAssignments>
<xt:envValueType>Service URI Weight</xt:envValueType>
<xt:location>0</xt:location>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:owner>
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
</cus:envValueAssignments>
<cus:envValueAssignments>
<xt:envValueType>Service URI</xt:envValueType>
<xt:location>0</xt:location>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:owner>
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">jejb:oracle:test</xt:value>
</cus:envValueAssignments>
<cus:envValueAssignments>
<xt:envValueType>Service URI Table</xt:envValueType>
<xt:location xsi:nil="true"/>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:owner>
<xt:value>
<tableElement xmlns="http://www.bea.com/wli/sb/transports">
<URI>jejb:oracle:test</URI>
<weight>0</weight>
</tableElement>
</xt:value>
</cus:envValueAssignments>
<cus:envValueAssignments>
<xt:envValueType>Service Retry Count</xt:envValueType>
<xt:location xsi:nil="true"/>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</xt:owner>
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
</cus:envValueAssignments>
<cus:envValueAssignments>
<xt:envValueType>Service Retry Iteration Interval</xt:envValueType>
<xt:location xsi:nil="true"/>
<xt:owner>
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Mytest</xt:path>
</xt:owner>
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">40</xt:value>
</cus:envValueAssignments>
</cus:customization>
<cus:customization xsi:type="cus:FindAndReplaceCustomizationType">
<cus:description/>
<cus:query>
<xt:resourceTypes>BusinessService</xt:resourceTypes>
<xt:envValueTypes>Service Retry Iteration Interval</xt:envValueTypes>
<xt:envValueTypes>Service URI Weight</xt:envValueTypes>
<xt:envValueTypes>Service Retry Count</xt:envValueTypes>
<xt:envValueTypes>Service URI</xt:envValueTypes>
<xt:refsToSearch xsi:type="xt:ResourceRefType">
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/MyTest</xt:path>
</xt:refsToSearch>
<xt:includeOnlyModifiedResources>false</xt:includeOnlyModifiedResources>
<xt:searchString>Search String</xt:searchString>
<xt:isCompleteMatch>false</xt:isCompleteMatch>
</cus:query>
<cus:replacement>Replacement String</cus:replacement>
</cus:customization>
<cus:customization xsi:type="cus:ReferenceCustomizationType">
<cus:description/>
<cus:refsToBeConsidered xsi:type="xt:ResourceRefType">
<xt:type>BusinessService</xt:type>
<xt:path>HelloWorld/BusinessServices/Test123</xt:path>
</cus:refsToBeConsidered>
<cus:externalReferenceMap>
<xt:oldRef>
<xt:type>ForeignJNDIProvider</xt:type>
<xt:path>System/JNDI Providers/oracle</xt:path>
</xt:oldRef>
<xt:newRef>
<xt:type>ForeignJNDIProvider</xt:type>
<xt:path>System/JNDI Providers/oracle</xt:path>
</xt:newRef>
</cus:externalReferenceMap>
<cus:externalReferenceMap>
<xt:oldRef>
<xt:type>Archive</xt:type>
<xt:path>dev/HelloOsbEjbClient</xt:path>
</xt:oldRef>
<xt:newRef>
<xt:type>Archive</xt:type>
<xt:path>dev/HelloOsbEjbClient</xt:path>
</xt:newRef>
</cus:externalReferenceMap>
</cus:customization>
</cus:Customizations>
答案 0 :(得分:2)
嗯,你可以用DocumentBuilder读取这两个文件。将节点存储在例如一些列表中,您可以在阅读它们后进行比较:
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fac.newDocumentBuilder();
Document doc = builder.parse(new File("a.xml");
之后,您可以从文档中获取所有节点(存储在NodeList中)
NodeList list = doc.getElementsbyName("someelement"); //just for an example
希望我能帮助你一点
答案 1 :(得分:2)
XMLUnit for Java提供了两个JUnit扩展类,XMLAssert和 XMLTestCase和一组支持类(例如Diff, DetailedDiff,Transform,SimpleXpathEngine,Validator,NodeTest)即 允许断言:
两段XML之间的差异
使用XSLT转换XML片段的结果
评估XML片段上的XPath表达式
一段XML的有效性
DOM Traversal公开的XML片段中的各个节点
XMLUnit for Java还可以将HTML内容(甚至是格式错误的HTML)视为有效的XML,以允许对这些内容进行这些断言。 网页内容也是如此。
可以找到另一个DocumentBuilder解决方案here。