I am using VBA in Excel. I have a XML file that has this structure:
<Level_A>
<Level_B>
<Level_C>First value</Level_C>
<Level_C>Second value</Level_C>
</Level_B>
<Level_B>
<Level_C>Third value</Level_C>
<Level_C>Fourth value</Level_C>
</Level_B>
</Level_A>
I want to
- loop through all
Level_Cnodes and to - store the (unique) position of each parent node within in the XML structure in an array.
There are two Level_B (parent) nodes in the above sample, so the first node would return position 0 and the second node position 1.
I would like these results:
- First value, Parent node position in XML tree is 0.
- Second value, Parent node position in XML tree is 0.
- Third value, Parent node position in XML tree is 1.
- Fourth value, Parent node position in XML tree is 1.
The Level_B (parent) nodes don't have any text or attributes.
Question
How can I find their (unique) position number in the XML tree?
A Level_B node can contain 0, 1 or 2 Level_C nodes. I need to get all Level_C nodes in the XML file and know the (unique) position number of their parent node.
I can read the basename of the parent node Level_B by using this code:
NodePath(i).ParentNode.BaseName
I'm looking for the same functionality but this time reading the unique position of the Level_B node.
Why am I asking this?
I have to read a lot of different XML files with financial data. All files contain the same type of information, but are structured in different ways. What all XML files have in common is that they have a financial value like $ 100.00.
I want to have flexible code so that I can just point to the nodepath of the financial value and then simply go up or down in the XML tree to find all the other data I need.
Below is an example of two of the XML structures I need to read (there are even more). I want to loop through the nodes containing the financial values and have the flexibility go up or down the XML tree to find the currency, transaction ID and a lot of other data. Sometimes I need to go up even 5 parent nodes to find what I need.
In this example there is no tag or attribute with a unique transaction ID:
<transactions>
<transaction>
<currency>USD</currency>
<financialvalues>
<value>100.00</value>
<value>200.00</value>
</financialvalues>
</transaction>
<transaction>
<currency>USD</currency>
<financialvalues>
<value>300.00</value>
<value>400.00</value>
</financialvalues>
</transaction>
</transactions>
In this other XML structure there is a unique transaction ID in the tag TransactionID. (Which makes it easier...)
<transactions>
<transaction>
<transactionID>5</transactionID>
<lines>
<line>
<financialvalue>100.00</value>
<currency>USD</currency>
</line>
<line>
<financialvalue>200.00</value>
<currency>USD</currency>
</line>
</lines>
</transaction>
</transactions>
I don't want to write a seperate script for each XML structure, but have the flexibility to just loop through the FinancialValue tags of each XML file and then go up or down to find what I need.
So in the first XML structure there is no tag or attribute telling me the unique TransactionID. Therefore I would like to access the position number of the parent Transaction so that I know which FinancialValue belongs to which transaction.