Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I count through child elements when reading an xml file

Status
Not open for further replies.

Riksta

Programmer
Dec 7, 2003
16
GB
Hi,

I am trying to read xml data using asp! I have this xml code:

<?xml version=&quot;1.0&quot; ?>

<persons>
<person>
<fname> joe </fname>
<sname> smith </sname>
<roomno> E127 </roomno>
<processor> Pentium 4 </processor>
<memory> 512 mb ram </memory>
<hardisk> 40 gb hard disk space </hardisk>
<cd_writer> CD Writer 8-4-32 </cd_writer>
<printer> Bubble Jet 450C </printer>
<description> The computer was purchased in 1999 and is due for an upgrade in 2004 </description>
</person>

I can read it fine with:

strfname = objXML.documentElement.firstChild.firstChild.text
strsname = objXML.documentElement.firstChild.childNodes(1).text
strroomno = objXML.documentElement.firstChild.childNodes(2).text
strprocessor = objXML.documentElement.firstChild.childNodes(3).text
strmemory = objXML.documentElement.firstChild.childNodes(4).text
strhardisk = objXML.documentElement.firstChild.childNodes(5).text
strcd_writer = objXML.documentElement.firstChild.childNodes(6).text
strprinter = objXML.documentElement.firstChild.childNodes(7).text
strdescription = objXML.documentElement.firstChild.childNodes(8).text

THE PROBLEM IS: What number child node would be the next bit????

<person>
<fname> Ian </fname>
<sname> Wright </sname>
<roomno> E130 </roomno>
<processor> Pentium 4 </processor>
<memory> 512 mb ram </memory>
<hardisk> 40 gb hard disk space </hardisk>
<cd_writer> CD Writer 8-4-32 </cd_writer>
<printer> Bubble Jet 450C </printer>
<description> The computer was purchased in 1999 and is due for
an upgrade in 2004 </description>
</person>

Would I start from 9, following on from the previous or would I start from 0....??

Please advise me.

Thanks

Riks
 
What are you using to read the xml file? A DOM (using XMLLoad method) or the SAX parser?

If you're using a DOM, you can do selectNodeList with a selectSingleNode using XPath queries to get your nodes:
Code:
objDOM.Load(c:\somefile.xml)
Set nodeList = objDom.selectNodeList(&quot;Person&quot;)
For each personNode in nodeList
   set node = personNode.selectSingleElement(&quot;fname&quot;)
   If not node Is Nothing Then
      sFirstName = node.Text
   Else
      sFirstName = &quot;&quot;    'or error if it's a required element
   End If
   set node = personNode.SelectSingleElement(&quot;sname&quot;)
   If not node Is Nothing Then
      sSName = node.Text
   Else
      sSName = &quot;&quot;    'or error if it's a required element
   End If

   ' and so forth for room number, processor, etc.
Next
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hi,

Thanks for that, so should I replace this code:

strfname = objXML.documentElement.firstChild.firstChild.text
strsname = objXML.documentElement.firstChild.childNodes(1).text
strroomno = objXML.documentElement.firstChild.childNodes(2).text
strprocessor = objXML.documentElement.firstChild.childNodes(3).text
strmemory = objXML.documentElement.firstChild.childNodes(4).text
strhardisk = objXML.documentElement.firstChild.childNodes(5).text
strcd_writer = objXML.documentElement.firstChild.childNodes(6).text
strprinter = objXML.documentElement.firstChild.childNodes(7).text
strdescription = objXML.documentElement.firstChild.childNodes(8).text

With the code you have given me???

Thanks

Rik
 
Accessing the nodes by numeric index will work, but it makes it difficult to handle repeating elements (as you've found). Also, if there's a chance that one or more of the elements are optional (say, <printer> is left out if the person doesn't have a printer), then that throws your count off.

Plus, attempting to access the .Text property of a missing element will cause an error, which is my my code always checks to see if you got anything back from the call to selectSingleNode().

One good idea would be to encapsulate all the code dealing with a person's PC configuration in it's own class. You can then pass it a string containing some XML (the parts between <person> and </person>), and let the class deal with extracting all the info from it. Afterwards, all the values could be available via public properties.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top