SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    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>
    Here is part of my ASP page:
    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
              
    %>
    My question relates to the code in the "For each el4 in ..." section. You see that I iterate through the childnodes until I find "value". Then I output that tag value and, as I know the next tag (for now until they change it), is the "units" tag I use nextsibling to get that value. This code works!

    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
    However, it did not work for me. I tried several variations: without the @ sign and with a full path in the oNode statements, ie /MyWeather/sensor/reading/...

    My check for empty oNode and/or O2Node always is true.

    Any ideas?.....RDK

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •