0

I have two XML's and i need differnece between those XML's as explained below:

F6.XML:

<MetaData xmlns="urn:wco:datamodel:WCO:DocumentMetaData-DMS:2">
  <Declaration xmlns="urn:wco:datamodel:WCO:DEC-DMS:2">
    <FunctionCode>9</FunctionCode>
    <FunctionalReferenceID>432189615698-46239</FunctionalReferenceID>
    <TypeCode>EXD</TypeCode>
    <GoodsItemQuantity>1</GoodsItemQuantity>
    <InvoiceAmount currencyID="GBP">7001.00</InvoiceAmount>
  </Declaration>
</MetaData>

F7.xml:

<MetaData xmlns="urn:wco:datamodel:WCO:DocumentMetaData-DMS:2">
  <Declaration xmlns="urn:wco:datamodel:WCO:DEC-DMS:2">
  <FunctionCode>9</FunctionCode>
  <AdditionalInformation>
        <StatementCode>AG001</StatementCode>
        <StatementDescription>AG001</StatementDescription>
        <Pointer>
            <SequenceNumeric>1</SequenceNumeric>
            <DocumentSectionCode>42A</DocumentSectionCode>
        </Pointer>
  </AdditionalInformation>
  <FunctionalReferenceID>432189615698-46239</FunctionalReferenceID>
  <GoodsItemQuantity>11</GoodsItemQuantity>
  <InvoiceAmount currencyID="GBP">7001.00</InvoiceAmount>
  </Declaration>
</MetaData>

I used below code to execute it:

Diff diffs = 
DiffBuilder.compare(xml1).ignoreWhitespace().ignoreWhitespace().             
withNodeMatcher(new 
DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes))
            . withTest(xml2).build();
    
    Iterator<Difference> iterator = 
diffs.getDifferences().iterator();
    while (iterator.hasNext()) {
        Difference nextDiff = iterator.next();
        System.out.println("-------------------->"+nextDiff);
    }

...and Actual Output was

    -------------------->Expected child nodelist sequence '1' but was '2' - comparing <FunctionalReferenceID...> at /MetaData[1]/Declaration[1]/FunctionalReferenceID[1] to <FunctionalReferenceID...> at /MetaData[1]/Declaration[1]/FunctionalReferenceID[1] (SIMILAR)
-------------------->Expected text value '1' but was '11' - comparing <GoodsItemQuantity ...>1</GoodsItemQuantity> at /MetaData[1]/Declaration[1]/GoodsItemQuantity[1]/text()[1] to <GoodsItemQuantity ...>11</GoodsItemQuantity> at /MetaData[1]/Declaration[1]/GoodsItemQuantity[1]/text()[1] (DIFFERENT)
-------------------->Expected child '{urn:wco:datamodel:WCO:DEC-DMS:2}TypeCode' but was 'null' - comparing <TypeCode...> at /MetaData[1]/Declaration[1]/TypeCode[1] to <NULL> (DIFFERENT)
-------------------->Expected child 'null' but was '{urn:wco:datamodel:WCO:DEC-DMS:2}AdditionalInformation' - comparing <NULL> to <AdditionalInformation...> at /MetaData[1]/Declaration[1]/AdditionalInformation[1] (DIFFERENT)

Expected Result Would Be:

//MetaData[1]/Declaration[1]/AdditionalInformation[1]/StatementCode[1]='AG001'
//MetaData[1]/Declaration[1]/AdditionalInformation[1]/StatementDescription[1]='AG001'
//MetaData[1]/Declaration[1]/AdditionalInformation[1]/Pointer[1]/SequenceNumeric[1]='1'
//MetaData[1]/Declaration[1]/AdditionalInformation[1]/Pointer[1]/DocumentSectionCode[1]='42A'

Note: We are fine with modified values (11) and deleted node TypeCode. But we need in detail XPATH of what was added additionally, like it's shown in the expected results section.

4
  • What have you tried? You can probably do something with XmlUnit. Try that and get back if you have further questions. Commented Nov 28, 2023 at 13:24
  • Diff diffs = DiffBuilder.compare(xml1).withTest(xml2).build(); Iterator<Difference> iterator = diffs.getDifferences().iterator(); while (iterator.hasNext()) { Difference nextDiff = iterator.next(); System.out.println("-------------------->"+nextDiff); } But not getting desired Output. In simple i need to compare and find out newly added nodes/attributes/elemnts and deleted ones and modified values in the Other XML Document (XML B) Commented Nov 29, 2023 at 7:10
  • 1
    It's better if you update/edit the question with properly formatted code Commented Nov 29, 2023 at 11:26
  • @forty-two : Question has been updated now. Could you please help ? Commented Nov 30, 2023 at 14:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.