-1

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

rrk
  • 15,677
  • 4
  • 29
  • 45
Süresh AK
  • 344
  • 3
  • 20

3 Answers3

2

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>
Tim Jasko
  • 1,532
  • 1
  • 8
  • 12
2

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>
Vlad
  • 1,077
  • 2
  • 13
  • 27
1

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