Grabbing the elements from the XML root node
Hello, I haven't worked with XML in over a year and I didn't use it much then. I'm trying to remember the best way to get the values from the root note (not child nodes).
For instance, in the following xml, I want the timestamp and the messageID from stumessages.
I can't recall if you are supposed to use xmlsearch or something else. I know how to grab the child nodes (for some reason, that part was easier to recall).
<cfsavecontent variable="XMLFile"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stuMessages SYSTEM "http://cody.glpconnect.com/DTD/StuMessage_Rev6.dtd">
<stuMessages timeStamp="14/05/2012 08:02:11 GMT" messageID="9768c7201b6f100585fbfa3243489116">
<stuMessage>
<esn>0-999999</esn>
<unixTime>1336982529</unixTime>
<gps>N</gps>
<payload length="9" source="pc" encoding="hex">0x3530E2B18801031200</payload>
</stuMessage>
</stuMessages>
</cfsavecontent>
<!--- Set the XML to a XML parsed variable we can use --->
<cfset MyXMLDoc = xmlParse(XMLFile) />
<!--- Dump the XML --->
<h2>Dump</h2>
<!--- <cfdump var="#MyXMLDoc#"> --->
<!--- Get all Message Nodes --->
<cfset messageNodes = xmlSearch(MyXMLDoc,'/stuMessages/stuMessage')>
<cfoutput>
<h2>Message Nodes</h2>
<cfloop from="1" to="#arraylen(messageNodes)#" index="i">
<!--- The array contents need to parsed so you can easily get at the child nodes children and attributes. --->
<cfset messageXML = xmlparse(messageNodes) />
<b>esn:</b> #messageXML.stuMessage.esn.xmltext#<br>
<b>unixTime:</b> #messageXML.stuMessage.unixTime.xmlText#<br>
<b>gps:</b> #messageXML.stuMessage.gps.xmlText#<br>
<b>payload:</b> #messageXML.stuMessage.payload.xmlText#<br><br>
</cfloop>
</cfoutput>
Best regards
KR
