Processing of xml data inside coldfusion

91 Views Asked by At

After processing data I can able to set the xml value

<cfoutput>#xmlResults#</cfoutput>

output example :

<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="shiporder.xsd">
    <orderperson>John Smith</orderperson>
    <shipto>
        <name>Ola Nordmann</name>
        <address>Langgt 23</address>
        <city>4000 Stavanger</city>
        <country>Norway</country>
    </shipto>
    <item>
        <title>Empire Burlesque</title>
        <note>Special Edition</note>
        <quantity>1</quantity>
        <price>10.90</price>
    </item>
    <item>
        <title>Hide your heart</title>
        <quantity>1</quantity>
        <price>9.90</price>
    </item>
</shiporder>

How I can use the details separately using coldfusion ?(I want to use the title value inside my application.)

3

There are 3 best solutions below

0
On BEST ANSWER

<cfsavecontent variable="variable.xmlResults">
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="shiporder.xsd">
    <orderperson>John Smith</orderperson>
    <shipto>
        <name>Ola Nordmann</name>
        <address>Langgt 23</address>
        <city>4000 Stavanger</city>
        <country>Norway</country>
    </shipto>
    <item>
        <title>Empire Burlesque</title>
        <note>Special Edition</note>
        <quantity>1</quantity>
        <price>10.90</price>
    </item>
    <item>
        <title>Hide your heart</title>
        <quantity>1</quantity>
        <price>9.90</price>
    </item>
</shiporder>
</cfsavecontent>
<cfset variables.myXmlArray = xmlparse(trim(variable.xmlResults))>

<cfset variables.responseNodesItem = XMLSearch(variables.myXmlArray,"//*[ local-name() = 'item' ]") />
<cfset variables.title = variables.responseNodesItem[1].title["XmlText"]> 
<cfdump var="#variables.myXmlArray#">
<cfdump var="#variables.responseNodesItem#">
<cfdump var="#variables.title#">
<cfabort>

3
On

After parsing, you can use XPath to search, and/or refer to elements in the xml document directly:

<cfset doc = XmlParse(xmlResults)>
<cfloop index="node" array="#XmlSearch(doc, '//item')#">
    <p>
        #node.title.xmlText#
    </p>
</cfloop>
0
On

You probably know the format of the xml and you can loop over the items

<cfset xmlParsed = xmlparse(xmlResults) >

<cfoutput>

<cfloop from="1" to="#arraylen(xmlParsed.shiporder.item)#" index="i">   

        title: #xmlParsed.shiporder.item[i].title.xmltext#<br>      

        <cfif structkeyExists(xmlParsed.shiporder.item[i],"note")>
            note: #xmlParsed.shiporder.item[i].note# <br> 
        </cfif>     

        <br>
</cfloop>

</cfoutput>