Compare differences between two XML's for XPATH attribute/element and values

71 views Asked by At

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.

0

There are 0 answers