I have 2 xml files a.xml
and b.xml
, b.xml
is modified version of a.xml
where some node values have been changed. Now I want to make a check in my code only particular node values can be changed not all the nodes. How can I make this check in java? I am getting this xml from user and he is allowed to change only particular nodes if he changes other nodes I should not allow .Below are xml You can see
我有两个xml文件a。xml和b。xml,b。xml是a的修改版本。更改了一些节点值的xml。现在我想在我的代码中做一个检查只有特定的节点值可以更改而不是所有的节点。我如何用java进行检查?我从用户那里得到了这个xml,如果他更改了我不允许的其他节点,那么他只允许更改特定的节点。
<xt:path>HelloWorld/BusinessServices/Mytest</xt:path>
has changed in both this is valid case where as he has also changed
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">40</xt:value>
from 30 to 40 which is not valid.
a.xml
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
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>
2 个解决方案
#1
2
Hmm may you read both files, with the DocumentBuilder. Store the nodes in for example some list that you can compare after reading them:
嗯,可以用DocumentBuilder读取这两个文件。将节点存储在一些列表中,这些列表可以在阅读后进行比较:
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fac.newDocumentBuilder();
Document doc = builder.parse(new File("a.xml");
After that you can get all the nodes out of your document (stored in a NodeList)
之后,您可以从文档中获取所有节点(存储在NodeList中)
NodeList list = doc.getElementsbyName("someelement"); //just for an example
Hope that I could help you a little
希望我能帮你一点忙
#2
2
Try XMLUnit for Java.
试着用Java。
XMLUnit for Java provides two JUnit extension classes, XMLAssert and XMLTestCase, and a set of supporting classes (e.g. Diff, DetailedDiff,Transform,SimpleXpathEngine,Validator,NodeTest) that allow assertions to be made about:
为Java提供的XMLUnit提供了两个JUnit扩展类、XMLAssert和XMLTestCase,以及一组支持类(例如Diff、DetailedDiff、Transform、SimpleXpathEngine、Validator、NodeTest),这些都允许断言:
The differences between two pieces of XML
这两个XML片段之间的差异
The outcome of transforming a piece of XML using XSLT
使用XSLT转换XML的结果
The evaluation of an XPath expression on a piece of XML
对XML块的XPath表达式的求值
The validity of a piece of XML
一段XML的有效性
Individual nodes in a piece of XML that are exposed by DOM Traversal
由DOM遍历公开的XML片段中的单个节点
XMLUnit for Java can also treat HTML content (even badly-formed HTML) as valid XML to allow these assertions to be made about the content of web pages too.
Java的XMLUnit还可以将HTML内容(甚至格式不佳的HTML)视为有效的XML,从而允许对web页面的内容进行断言。
Another DocumentBuilder solution can be found here.
另一个DocumentBuilder解决方案可以在这里找到。
#1
2
Hmm may you read both files, with the DocumentBuilder. Store the nodes in for example some list that you can compare after reading them:
嗯,可以用DocumentBuilder读取这两个文件。将节点存储在一些列表中,这些列表可以在阅读后进行比较:
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fac.newDocumentBuilder();
Document doc = builder.parse(new File("a.xml");
After that you can get all the nodes out of your document (stored in a NodeList)
之后,您可以从文档中获取所有节点(存储在NodeList中)
NodeList list = doc.getElementsbyName("someelement"); //just for an example
Hope that I could help you a little
希望我能帮你一点忙
#2
2
Try XMLUnit for Java.
试着用Java。
XMLUnit for Java provides two JUnit extension classes, XMLAssert and XMLTestCase, and a set of supporting classes (e.g. Diff, DetailedDiff,Transform,SimpleXpathEngine,Validator,NodeTest) that allow assertions to be made about:
为Java提供的XMLUnit提供了两个JUnit扩展类、XMLAssert和XMLTestCase,以及一组支持类(例如Diff、DetailedDiff、Transform、SimpleXpathEngine、Validator、NodeTest),这些都允许断言:
The differences between two pieces of XML
这两个XML片段之间的差异
The outcome of transforming a piece of XML using XSLT
使用XSLT转换XML的结果
The evaluation of an XPath expression on a piece of XML
对XML块的XPath表达式的求值
The validity of a piece of XML
一段XML的有效性
Individual nodes in a piece of XML that are exposed by DOM Traversal
由DOM遍历公开的XML片段中的单个节点
XMLUnit for Java can also treat HTML content (even badly-formed HTML) as valid XML to allow these assertions to be made about the content of web pages too.
Java的XMLUnit还可以将HTML内容(甚至格式不佳的HTML)视为有效的XML,从而允许对web页面的内容进行断言。
Another DocumentBuilder solution can be found here.
另一个DocumentBuilder解决方案可以在这里找到。