Skip to main content
November 24, 2009
Question

How to read data from XML using ColdFusion

  • November 24, 2009
  • 3 replies
  • 1154 views

Hi,

Can anybody please help me how to read data (incident number) from the below mentioned XML file using ColdFusion?

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns0:HelpDesk_Submit_ServiceResponse xmlns:ns0="urn:HPD_IncidentInterface_Create_WS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:Incident_Number>INC000000021575</ns0:Incident_Number>
</ns0:HelpDesk_Submit_ServiceResponse>
</soapenv:Body>
</soapenv:Envelope>

In simple, I want to read/print the Incident_Number node value that is “INC000000021575” from the above XML file.

Thanks in advance,

Regards,
Manoz.

    This topic has been closed for replies.

    3 replies

    BKBK
    Community Expert
    Community Expert
    November 25, 2009

    <cfset incident_number = xmlDoc.xmlroot.xmlChildren[1].xmlChildren[1].xmlChildren[1].xmlText>

    November 26, 2009

    When I try to use the above mentioned solutions that are suggested by Ian & BKBK CF server is throwing an error like "You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members"

    However the URL provided by Ian Skinner was useful & I was able to read the value properly.

    Below you can find the code for reference.

    <cfsavecontent variable="strXml">

    <?xml version="1.0" encoding="UTF-8" ?>

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns0:HelpDesk_Submit_ServiceResponse xmlns:ns0="urn:HPD_IncidentInterface_Create_WS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <ns0:Incident_Number>INC000000021575</ns0:Incident_Number>

    </ns0:HelpDesk_Submit_ServiceResponse></soapenv:Body></soapenv:Envelope>

    </cfsavecontent>

    <cfset xmlRequest = XmlParse(strXml.Trim()) />

    <!--- Search for the "searchFor" XML node using XPath. --->

    <cfset arrSearchNodes = XmlSearch(xmlRequest,"//*[name()='ns0:Incident_Number']") />

    <cfset IncidentID = #arrSearchNodes[1]["XmlText"]#>

    Thanks to everyone.

    Regards,

    Manoz.

    BKBK
    Community Expert
    Community Expert
    November 26, 2009

    When I try to use the above mentioned solutions that are suggested by Ian & BKBK CF server is throwing an error like "You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members"

    It's obvious why you got the error. You were almost there. The tag cfsavecontent gives you a string, whereas you need an XML document object in the suggestions we gave you.

    <cfset xmlRequest = XmlParse(strXml.Trim()) />
    <!--- Search for the "searchFor" XML node using XPath. --->
    <cfset arrSearchNodes = XmlSearch(xmlRequest,"//*[name()='ns0:Incident_Number']") />
    <cfset IncidentID = #arrSearchNodes[1]["XmlText"]#>

    That would work, but xmlparse and xmlSearch are costly in performance. This is what the intention was in my earlier suggestion:

    <cfxml variable="xmlDoc">
    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
    <ns0:HelpDesk_Submit_ServiceResponse xmlns:ns0="urn:HPD_IncidentInterface_Create_WS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns0:Incident_Number>INC000000021575</ns0:Incident_Number>
    </ns0:HelpDesk_Submit_ServiceResponse>
    </soapenv:Body>
    </soapenv:Envelope>
    </cfxml>

    <cfset incident_number = xmlDoc.xmlroot.xmlChildren[1].xmlChildren[1].xmlChildren[1].xmlText>

    Inspiring
    November 24, 2009

    CF has a fairly broad range of XML-handling functions.  Start by having a nosey through them:

    http://livedocs.adobe.com/coldfusion/8/htmldocs/functions-pt0_21.html#3468770

    --

    Adam

    ilssac
    Inspiring
    November 24, 2009

    Array Notation is one way.

    <cfoutput>#xmlVar["soapenv:Envelope"]["sopaEnv:Body"]["ns0:HelpDesk_Submit_ServiceResponse"]["ns0:Inccident_Number"]#</cfoutput>

    If you want to use ColdFusion XML functions, you need to read up about using name spaces with ColdFusion.

    http://www.google.com/search?q=coldFusion+xml+namespace