Skip to main content
May 14, 2012
Question

Grabbing the elements from the XML root node

  • May 14, 2012
  • 1 reply
  • 841 views

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

    This topic has been closed for replies.

    1 reply

    May 14, 2012

    Hello,

    I figured it out.

    I figured I would update my post in case anyone else was curious.

    I just need to add the following two lines into the code to expose the elements of the root node.

        <cfset timeStamp = MyXMLDoc.stuMessages.XmlAttributes.timeStamp>

        <cfset messageid = MyXMLDoc.stuMessages.XmlAttributes.messageID>

    The full code follows

    <!--- 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>

        <cfset timeStamp = MyXMLDoc.stuMessages.XmlAttributes.timeStamp>

        <cfset messageid = MyXMLDoc.stuMessages.XmlAttributes.messageID>

        #timeStamp#<br />

        #messageid#<br />

        <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>

    BKBK
    Community Expert
    Community Expert
    May 15, 2012

    Possible alternatives:

    <cfset timeStamp = MyXMLDoc.XmlRoot.XmlAttributes['timeStamp']>

    <cfset messageid = MyXMLDoc.XmlRoot.XmlAttributes['messageID']>

    <cfset timeStamp = MyXMLDoc.XmlRoot.XmlAttributes.timeStamp>

    <cfset messageid = MyXMLDoc.XmlRoot.XmlAttributes.messageID>

    <cfset timeStamp = xmlSearch(MyXMLDoc,'/stuMessages/@timeStamp')[1].Xmlvalue>

    <cfset messageid = xmlSearch(MyXMLDoc,'/stuMessages/@messageID')[1].Xmlvalue>