1

我一直在尝试找出使用 Coldfusion 从 xml 文件中提取数据的最佳方法。xml文件比较简单,但是搞清楚这个问题。XML 数据如下所示:

<recordname>
<biols>Perennial </biols>
<biomd>
common </biomd>
<catcn>Tibetan Flora Images</catcn>
<catot>Photo</catot>
<darfm>Araliaceae</darfm>
<dargn>Aralia</dargn>
<darsn>Aralia apioides</darsn>
<comnm>qing ye long yan du he</comnm>
<mulmm0>
<mulmm>
<irn>10221626</irn>
</mulmm>
<mulmm>
<irn>10221629</irn>
</mulmm>
<mulmm>
<irn>10221631</irn>
</mulmm>
<mulmm>
<irn>10221633</irn>
</mulmm>
<mulmm>
<irn>10221636</irn>
</mulmm>
</mulmm0>
</record>

我想将每个 mulmm irn 作为记录返回,并在该记录中包含相关的父信息,这样我就有 5 条带有 irn 的记录,并且每条记录中都是记录名称信息。最好的方法是什么?

我为这个简单的问题道歉 - 出于某种原因,这让我很困惑。谢谢你的帮助!

4

1 回答 1

2

不确定这是你想要的,但这是我的 2 美分:

<cfset myDoc ="<recordname>
<biols>Perennial </biols>
<biomd>
common </biomd>
<catcn>Tibetan Flora Images</catcn>
<catot>Photo</catot>
<darfm>Araliaceae</darfm>
<dargn>Aralia</dargn>
<darsn>Aralia apioides</darsn>
<comnm>qing ye long yan du he</comnm>
<mulmm0>
<mulmm>
<irn>10221626</irn>
</mulmm>
<mulmm>
<irn>10221629</irn>
</mulmm>
<mulmm>
<irn>10221631</irn>
</mulmm>
<mulmm>
<irn>10221633</irn>
</mulmm>
<mulmm>
<irn>10221636</irn>
</mulmm>
</mulmm0>
</recordname>">

<cfset myXML = XMLParse(myDoc)>

<cfset numRCItems = ArrayLen(myXML.recordname.XMLChildren)>
<cfset numMulmmItems = ArrayLen(myXML.recordname.mulmm0.XMLChildren)>

<cfset myQuery = QueryNew("irn, biols, biomd, catcn, catot, darfm, dargn, darsn, comnm") >
<cfset temp = QueryAddRow(myQuery, #numMulmmItems#)>

<cfloop index="i" from = "1" to = #numMulmmItems#>
    <cfset temp = QuerySetCell(myQuery, "irn", #myXML.recordname.mulmm0.mulmm[i].irn.XMLText#,#i#)>
        <cfloop index="j" from = "1" to = #numRCItems#>
            <cfif myXML.recordname.XMLChildren[j].XMLName IS NOT "mulmm0">
                <cfset temp = QuerySetCell(myQuery, myXML.recordname.XMLChildren[j].XMLName, #myXML.recordname.XMLChildren[j].XMLtext#,#i#)>    
            </cfif>
        </cfloop> 
</cfloop>

<cfdump var="#myQuery#" >
于 2012-07-13T10:18:44.597 回答