-1

处理数据后,我可以设置 xml 值

<cfoutput>#xmlResults#</cfoutput>

输出示例:

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

如何使用coldfusion单独使用详细信息?(我想在我的应用程序中使用标题值。)

4

3 回答 3

2

您可能知道 xml 的格式,并且可以遍历项目

<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>
于 2016-02-16T12:35:14.847 回答
2

解析后,您可以使用 XPath搜索,和/或直接引用 xml 文档中的元素:

<cfset doc = XmlParse(xmlResults)>
<cfloop index="node" array="#XmlSearch(doc, '//item')#">
    <p>
        #node.title.xmlText#
    </p>
</cfloop>
于 2016-02-15T13:07:05.293 回答
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>

于 2016-04-21T11:26:33.533 回答