SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
-
Jun 1, 2013, 09:24 #1
- Join Date
- Jun 2013
- Posts
- 1
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How can I retrieve a Specific element value from an XML file?
Folks....Probably a simple question, but I have not been able to find the simple answer. How can I retrieve a specific value from an XML structure without travelling down the structure and iterating through all of the childnodes?
Assume the following simplified XML structure:
Code:MyWeather> <sensor location="Front Entry"> <reading label="Temperature"> <title>Front Entry</title> <label>Temperature</label> <value>54</value> <units>F</units> <lastUpdate>05/27/2013 12:23 AM</lastUpdate> </reading> <reading label="Humidity"> <title>Front Entry</title> <label>Humidity</label> <value>66</value> <units>%</units> <lastUpdate>05/27/2013 12:23 AM</lastUpdate> </reading> </sensor> <sensor location="Patio"> <reading label="Temperature"> <title>Patio</title> <label>Temperature</label> <value>46</value> <units>F</units> <lastUpdate>05/27/2013 12:23 AM</lastUpdate> </reading> <reading label="Humidity"> <title>Patio</title> <label>Humidity</label> <value>93</value> <units>%</units> <lastUpdate>05/27/2013 12:23 AM</lastUpdate> </reading> </sensor> </MyWeather>
Code:<% xmlDoc = CreateObject("Msxml2.DOMDocument") xmldoc.setProperty ("ServerHTTPRequest", true) xmlDoc.setProperty ("ProhibitDTD", False) xmlDoc.validateOnParse = true ' Filename is a sting variable containing the URL xmlDoc.async = "False" xmlDoc.load(FileName) ' Error and return code processing done here but rewmoved For Each el In xmldoc.childNodes if el.childnodes.length <> 0 then response.write ("<table align='center' class='auto-style1'>") for each el2 in el.childnodes Response.write("<tr><td>" & el2.getattribute("location") & "</td><td></td></tr>") for each el3 in el2.childnodes for each el4 in el3.childnodes if el4.nodename = "value" then Response.write("<tr><td></td><td>" & el3.getattribute("label") & " " & el4.text & " " & el4.nextSibling.text & "</td></tr>") exit for end if next next next response.write ("</table>") end if Next xmlDoc = Nothing %>
What I would like to know is:
Is there a more direct way to these two tag values for any combination of Sensor location and Reading label without my iteration process.
I have several other cases where I may need to iterate through more than 50 elements to find the tag I'm looking for.
Based on a suggestion that I should be using XPath, I added this code . This I thought would replace the "for each el4 in el3.childnodes loop" above
Code:xmlDoc.setProperty ("SelectionLanguage", "XPath") oNode = xmldoc.selectSingleNode("//reading[@label='Temperaure']/units") o2Node = xmldoc.selectSingleNode("//reading[@label='Temperaure']/value") if oNode is nothing or o2node is nothing then Response.write("<tr><td></td><td> Nothing found for value or units </td><td>" & el3.getattribute("label") & "</tr>") else Response.write("<tr><td></td><td>" & el3.getattribute("label") & " " & o2Node.text & " " & oNode.text & "</td></tr>") end if
My check for empty oNode and/or O2Node always is true.
Any ideas?.....RDK
-
Jun 4, 2013, 07:16 #2
Bookmarks