Reputation: 5247
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
<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
<?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>
Upvotes: 1
Views: 575
Reputation: 6094
Try XMLUnit for 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:
The differences between two pieces of XML
The outcome of transforming a piece of XML using XSLT
The evaluation of an XPath expression on a piece of XML
The validity of a piece of XML
Individual nodes in a piece of XML that are exposed by DOM Traversal
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.
Another DocumentBuilder solution can be found here.
Upvotes: 2
Reputation: 131
Hmm may you read both files, with the DocumentBuilder. Store the nodes in for example some list that you can compare after reading them:
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 list = doc.getElementsbyName("someelement"); //just for an example
Hope that I could help you a little
Upvotes: 2