【问题标题】:parsing xml with coldfusion用coldfusion解析xml
【发布时间】:2017-10-09 18:27:48
【问题描述】:

到目前为止,我已经使用以下内容解析出一段 xml,如下所示:

 <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解析这个?

【问题讨论】:

  • 您到底遇到了什么问题? report 有 n 个孩子可以循环:Report.XmlChildren
  • 对,但我只想循环遍历称为查询的孩子。

标签: xml coldfusion


【解决方案1】:

如果你保证“报告”的第一个孩子总是“其他部分”,你可以尝试这样的事情

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

【讨论】:

  • 对不起,我只是把它扔在那里来说明我的问题。我永远不会知道在询问之前会有多少孩子,或者会有多少孩子打电话询问。
  • 在这种情况下,您可以这样做:xmlSearch(yourXMLVariable, "report/inquiries")。这应该会给你一个包含所有“查询”节点的数组,然后你可以循环遍历
  • 听起来像我需要的,我试试看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多