I have an XML parsing question -- how do I efficiently write an algorithm that can proper parse through the <wp:address> node when it gives two different results? for example, one reverse phone lookup gives this:
in my application I am fetching the <wp:address> node but I explicitly extract the 'child nodes' by accessing their index, i.e., childnode(0), childnode(4), etc.. but that obviously gives me an error when parsing through the 2nd example above since the 1st result's information differs from the 2nd result.
Any help in this matter, as far as properly writing an algorithm, would be greatly appreciated. Thanks in advance.
One of the greatest strengths and weaknesses of XML is that you should never code anything to hit a specific position in XML. Now I know that in some cases you can count on the XML being identical, but my preference is to never count on that. It would be far easier to read in the address node then create a nodelist of all it's children. Then read those children's nodeName and create a variable array based upon the contained text node. It's hard to describe without a language preference, I am trying to be general in the description.
Pseudo'd something like:
// given address_xml = the address node
address_obj = {};
nodes = address_xml->childNodes;
for ( i=0;i<nodes.length;i++ ) {
....node = nodes.item(i);
....address_obj[node.nodeName] = node.textContent;
}
Thanks for the reply; that does make sense. Basically make a 'dictionary' object of all the child nodes and then extract the data as needed from that object.
in C#, is there a way to create an array with the index, being actual strings (as opposed to integer)? a la a dictionary object in VBScript?
I have an XML parsing question -- how do I efficiently write an algorithm that can proper parse through the <wp:address> node when it gives two different results? for example, one reverse phone lookup gives this:
<wp:address wp:deliverable="true">
<wp:fullstreet>1234 test dr</wp:fullstreet>
<wp:house>1234</wp:house>
<wp:street>test dr</wp:street>
<wp:streettype>dr</wp:streettype>
<wp:city>Some City</wp:city>
<wp:state>CA</wp:state>
<wp:zip>90210</wp:zip>
<wp:zip4>1234</wp:zip4>
<wp:country>US</wp:country>
</wp:address>
and another reverse phone lookup gives this:
<wp:address wp:deliverable="false">
<wp:city>SomeOtherCity</wp:city>
<wp:state>CA</wp:state>
</wp:address>
in my application I am fetching the <wp:address> node but I explicitly extract the 'child nodes' by accessing their index, i.e., childnode(0), childnode(4), etc.. but that obviously gives me an error when parsing through the 2nd example above since the 1st result's information differs from the 2nd result.
Any help in this matter, as far as properly writing an algorithm, would be greatly appreciated. Thanks in advance.
Message edited by Avazee 5 years ago
BaldPenguin – 5 years ago
One of the greatest strengths and weaknesses of XML is that you should never code anything to hit a specific position in XML. Now I know that in some cases you can count on the XML being identical, but my preference is to never count on that. It would be far easier to read in the address node then create a nodelist of all it's children. Then read those children's nodeName and create a variable array based upon the contained text node. It's hard to describe without a language preference, I am trying to be general in the description.
Pseudo'd something like:
// given address_xml = the address node
address_obj = {};
nodes = address_xml->childNodes;
for ( i=0;i<nodes.length;i++ ) {
....node = nodes.item(i);
....address_obj[node.nodeName] = node.textContent;
}
Is this what you need?
Avazee – 5 years ago
Thanks for the reply; that does make sense. Basically make a 'dictionary' object of all the child nodes and then extract the data as needed from that object.
in C#, is there a way to create an array with the index, being actual strings (as opposed to integer)? a la a dictionary object in VBScript?