使用 Coldfusion 查找所有 XML 节点

发布于 2024-10-07 21:09:41 字数 276 浏览 0 评论 0原文

我想使用 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>

I want to extract all the nodes(parent/children) with name "person" using coldfusion and make the value of the attribute "image1" as NULL

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2024-10-14 21:09:41

这是使用递归解决您的(编辑的)问题的解决方案:

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

希望有帮助!

Here is a solution to your (edited) question using recursion:

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

Hope that helps!

想你的星星会说话 2024-10-14 21:09:41
<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>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文