使用coldfusion查找所有XML节点

时间:2010-12-15 10:42:25

标签: xml coldfusion

我想使用coldfusion提取名为“person”的所有节点(父/子),并将属性“image1”的值设为NULL

<person image1="img1.jpg" image2="img2.jpg">
    <person image1="img3.jpg" image2="img4.jpg" />
    <person image1="img5.jpg" image2="img6.jpg" />
</person>

2 个答案:

答案 0 :(得分:4)

以下是使用递归解决您的(已编辑)问题的方法:

<cfset s = '<person image1="img1.jpg" image2="img2.jpg">
   <person image1="img3.jpg" image2="img4.jpg" />
   <person image1="img5.jpg" image2="img6.jpg" />
</person>' />

<cfset doc = xmlParse(s) />

<cfdump var="#doc#" label="before" />

<cfset myFunction(doc.xmlRoot) />

<cfdump var="#doc#" label="after" />

<!--- Function --->
<cffunction name="myFunction" output="true">
    <cfargument name="doc" type="xml" />

    <cfif ARGUMENTS.doc.xmlName eq "person">

        <cfloop collection="#ARGUMENTS.doc.xmlAttributes#" item="LOCAL.k">
            <p>#ARGUMENTS.doc.xmlAttributes[LOCAL.k]# set to null</p>
            <cfset ARGUMENTS.doc.xmlAttributes[LOCAL.k] = "null" />
        </cfloop>

    </cfif>

    <!--- Recursively call for children --->
    <cfloop array="#ARGUMENTS.doc.xmlChildren#" index="LOCAL.childElem">
        <cfset myFunction(LOCAL.childElem) />
    </cfloop>

</cffunction>

希望有所帮助!

答案 1 :(得分:1)

<cfscript>
doc = xmlparse(/*xml from above*/);
selectedElements = xmlsearch(doc, "//language");

for (i=1; i LTE ArrayLen(selectedElements); i = i + 1) {
   // do what you want with the nodes here 
}
</cfscript>