2017-10-09 94 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

聽起來像我需要的,我會試試看。 –