2017-10-09 66 views
0

我用以下到这一点解析出看起来像这样的XML的一部分:解析XML使用ColdFusion

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

什么我不知道的是有时XML就这样产生了:

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

我不知道很多其他的孩子怎么会在报告中还是多少查询标签会有,但我只需要解析的查询和他们的孩子。我怎样才能用coldfusion解析这个?

+0

究竟是什么,你有麻烦? '报告'有n个孩子,你可以通过循环:'Report.XmlChildren' – Alex

+0

对,但我特别只想循环通过称为查询的孩子。 –

回答

1

如果要保证“报告”的第一个孩子总是“otherSection”你可以尝试这样的事情

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

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

对不起,我只是在那里说明我的问题。在查询之前我不知道会有多少孩子,或者有多少孩子会打电话询问。 –

+1

在这种情况下,你可以这样做:'xmlSearch(yourXMLVariable,“report/queries”)'。这应该给你一个所有'查询'节点的数组,然后你可以循环通过 – ultimoTG

+0

听起来像我需要的,我会试试看。 –