4
\$\begingroup\$

I have put together this function which compares two XMLs. On debugging, I found out that there are lot of useless loops which might cause performance problems for me if the files to compare are very big in size. Is there a tweak that I can do to this function so that i can cut down on some (if not all) of the useless loops? Note that I am sending two sorted similar XMLs for comparison.

Call RecursiveNodeCompare(objXML1.DocumentElement, objXML2.DocumentElement) 
Private Sub RecursiveNodeCompare(objBase, objOut) If objBase.ChildNodes.Length = 0 Or objOut.ChildNodes.Length = 0 Then Exit Sub 'to break recursion Dim nodeOut, nodeBase, nodeMin, nodeMax, arrBase, arrOut For Each nodeBase In objBase.ChildNodes For Each nodeOut In objOut.ChildNodes If nodeOut.BaseName = nodeBase.BaseName Then 'if both sections are same If UCase(nodeOut.XML) = UCase(nodeBase.XML) Then 'if all childs of sections are same nodeOut.ParentNode.RemoveChild nodeOut nodeBase.ParentNode.RemoveChild nodeBase Exit For Else 'if sections same but child's differ - check the difference Call RecursiveNodeCompare(nodeBase, nodeOut) End If End If Next Next End Sub 

Base.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <TestTrade> <TradeHeader> <GiveGet/> <Action>New</Action> <SourceSystemRef>Test</SourceSystemRef> <User>admin</User> <TimeStamp>2017-03-21T10:01:26.819</TimeStamp> </TradeHeader> <PreTrade/> <TradeIds> <TradeId> <Id>1234</Id> <Version>1</Version> </TradeId> </TradeIds> <StandardDetail> <Trader>Pankaj</Trader> <TradeDate>2017-03-21T10:01:24.000</TradeDate> <Direction>Buy</Direction> <Nominal Currency="COP">12</Nominal> <Price PriceType="Price" PriceType1="Price1">114.527</Price> </StandardDetail> <Allocations> <FundAllocation> <FundRef Domain="Test">TestFund</FundRef> <Allocation>12</Allocation> </FundAllocation> </Allocations> <BackOffice> <Usage>None</Usage> <FacingPrimeBrokerRef Domain="None">XYZ</FacingPrimeBrokerRef> <CashAccounts> <CashAccount> <FundRef Domain="Test">TestFund</FundRef> <AccountRef Domain="Kondor">PQR</AccountRef> </CashAccount> <CashAccount> <FundRef Domain="Test">TestFund</FundRef> <AccountRef Domain="Counterparty">1231312</AccountRef> </CashAccount> </CashAccounts> </BackOffice> <Flags> <FlagValue Name="IsExpired">false</FlagValue> <FlagValue Name="IsFullyFunded">false</FlagValue> </Flags> <SpecificDetail> <Bond> <UnderlyingBondRef Domain="ISIN">ABCD1234</UnderlyingBondRef> <UnderlyingBondCurrency>COP</UnderlyingBondCurrency> </Bond> <FundingLeg> <LegHeader> <FixingFrequency>Quarterly</FixingFrequency> <Flags> <FlagValue Name="IsAdjusted">false</FlagValue> </Flags> <Stub>None</Stub> </LegHeader> <FixedRate PriceType="Rate">0</FixedRate> </FundingLeg> </SpecificDetail> </TestTrade>

Out.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <TestTrade> <TradeHeader> <GiveGet/> <Action>New</Action> <SourceSystemRef>Test</SourceSystemRef> <User>admin</User> <TimeStamp>2017-03-21T10:01:26.819</TimeStamp> </TradeHeader> <PreTrade/> <TradeIds> <TradeId> <Id>1234</Id> <Version>2</Version> </TradeId> </TradeIds> <StandardDetail> <Trader>Pankaj</Trader> <TradeDate>2017-03-21T10:01:24.000</TradeDate> <Direction>Buy</Direction> <Nominal Currency="COP">12</Nominal> <Price PriceType="Price">114.527</Price> </StandardDetail> <Allocations> <FundAllocation> <FundRef Domain="Test">TestFund</FundRef> <Allocation>12</Allocation> </FundAllocation> </Allocations> <BackOffice> <Usage>None</Usage> <FacingPrimeBrokerRef Domain="None">XYZ</FacingPrimeBrokerRef> <CashAccounts> <CashAccount> <FundRef Domain="Test">TestFund</FundRef> <AccountRef Domain="Kondor">PQR</AccountRef> </CashAccount> <CashAccount> <FundRef Domain="Test">TestFund</FundRef> <AccountRef Domain="Counterparty">1231312</AccountRef> </CashAccount> </CashAccounts> </BackOffice> <Flags> <FlagValue Name="IsExpired">false</FlagValue> </Flags> <SpecificDetail> <Bond> <UnderlyingBondRef Domain="ISIN">ABCD1234</UnderlyingBondRef> <UnderlyingBondCurrency>COP</UnderlyingBondCurrency> </Bond> <FundingLeg> <LegHeader> <FixingFrequency>Quarterly</FixingFrequency> <Flags> <FlagValue Name="IsAdjusted">false</FlagValue> <Extra>Test</Extra> </Flags> <Stub>None</Stub> </LegHeader> <FixedRate PriceType="Rate">0</FixedRate> </FundingLeg> </SpecificDetail> </TestTrade>

After running my function - (before running the function, I sort the XMLs via XSLT)

Base.xml

<TestTrade> <Flags> <FlagValue Name="IsFullyFunded">false</FlagValue> </Flags> <SpecificDetail> <FundingLeg> <LegHeader> <Flags/> </LegHeader> </FundingLeg> </SpecificDetail> <StandardDetail> <Price PriceType="Price" PriceType1="Price1"/> </StandardDetail> <TradeIds> <TradeId> <Version>1</Version> </TradeId> </TradeIds> </TestTrade>

Out.xml

<TestTrade> <Flags/> <SpecificDetail> <FundingLeg> <LegHeader> <Flags> <Extra>Test</Extra> </Flags> </LegHeader> </FundingLeg> </SpecificDetail> <StandardDetail> <Price PriceType="Price"/> </StandardDetail> <TradeIds> <TradeId> <Version>2</Version> </TradeId> </TradeIds> </TestTrade>

\$\endgroup\$
4
  • \$\begingroup\$Not knowing the MSXML DOM, but if the input documents are sorted, the DOMs should be yeah? In which case, could you get away with only comparing the like children of each tree in order, rather than checking against every childnode at the same path?\$\endgroup\$CommentedApr 11, 2017 at 3:39
  • \$\begingroup\$The docs are sorted but they might differ in contents - text or missing nodes. Hence comparing the depth.\$\endgroup\$CommentedApr 11, 2017 at 5:20
  • 3
    \$\begingroup\$There are a zillion different ways to compare two XML documents depending what you consider significant: before we can comment on your code, we need to see your definition of "sameness". But certainly, your algorithm needs a lot of work not only to make it efficient, but also to make it robust.\$\endgroup\$CommentedApr 11, 2017 at 7:54
  • \$\begingroup\$@MichaelKay - Hi Micheal, I've added sample xmls.\$\endgroup\$CommentedApr 11, 2017 at 9:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.