Kara
Kara

Reputation: 55

How to parse through each XML element/XML children in Coldfusion

I am trying to get the xmltext of the children for each element (there are 377 elements). The inner loop is what I am unsure about. I don't even know if I am doing it right. Thank you so much for any help. I have attached an image as well so you can see the XML Structure.

Code:

   <cfhttp url="https://company.company.com/company.svc/GetXML/16/" username="test" password="test" method="GET" result="headingsCode">
    </cfhttp>

    <cfset deliverablesXML = xmlparse(headingsCode.filecontent)>

    <cfset packageNodes = XmlSearch(deliverablesXML, "//env:Envelope/env:Reports/env:Report/dl:Contracts/dl:Contract/")/>

    <cfloop index="i" from="1" to="#arrayLen(packageNodes)#">
    <cfloop from="1" to="#arrayLen(packageNodes[i].XmlChildren)#" index="ii">

       ?????????????????????

    </cfloop>
</cfloop>

An example of one of the elements

Upvotes: 3

Views: 841

Answers (1)

Alex
Alex

Reputation: 7833

I suggest you to nest foreach loops. It's easier to maintain node level by names than by indices. Example:

<cfset dlNodes = []>

<cfloop array="#packageNodes#" index="xmlNode">
    <cfloop array="#xmlNode.XmlChildren#" index="xmlNodeChild">

        <cfset dlNodes.add(
            (xmlNodeChild.XmlName & ": " & xmlNodeChild.XmlText)
        )>

        <!--- uncomment line below to debug a single XML node --->
        <!--- <cfdump var="#xmlNodeChild#"><cfabort> --->

    </cfloop>
</cfloop>

Not a good example for descriptive node names since I don't know what kind of data you are even working with, but you get the idea.

Upvotes: 3

Related Questions