Patrick
Patrick

Reputation: 2577

parsing xml with coldfusion

I've used the following up to this point to parse out a section of an xml that looks like this:

 <report>
      <otherSections>
      </otherSections>
      ...
      <inquiries>
          <inquiry>
             <date>01/01/06</date>
          </inquiry>
          ..more inquiries
      </inquiries>
      ..more sections
  </report>

      <cfset numInquiries = ArrayLen(Report.inquiries.XmlChildren) >
      <cfloop index="i" from = "1" to = "#numInquiries#" >
        <cfset strInquiryID =  Report.inquiries.inquiry[i].date.XMLText/>

      </cfloop>

What I didn't realize is sometimes the xml comes like this:

        <report>
           <otherSections>
           </otherSections>
           ...
          <inquiries>
              <inquiry>
                 <date>02/01/06</date>
              </inquiry>
              ..more inquiries
          </inquiries>
          <inquiries>
              <inquiry>
                 <date>01/01/06</date>
              </inquiry>
              ..more inquiries
          </inquiries>
          ..more sections
      </report>

I won't know how many other children will be in report or how many inquiries tags there will be, but I only want to parse the inquiries and their children. How can I parse this with coldfusion ?

Upvotes: 0

Views: 1099

Answers (1)

ultimoTG
ultimoTG

Reputation: 953

If you're guaranteed that the first child of 'report' is always 'otherSection' you could try something like this

<cfloop from="2" to="#arrayLen(test.report.xmlChildren)#" index="i">
    <cfset strInquiryID =  test.report.xmlChildren[i].inquiry.date.xmlText />
</cfloop>

or

<cfloop array="#test.report.xmlChildren#" index="i">
    <cfif i.xmlName neq 'otherSection'>
        <cfset strInquiryID =  i.inquiry.date.xmlText />
    </cfif>
</cfloop>

Upvotes: 1

Related Questions