xml - 使用 Coldfusion 解析复杂的 XML 文件

标签 xml coldfusion xml-parsing coldfusion-9

我正在解析包含车辆数据和规范的 XML 文件。我在循环某些节点以获取多个值时遇到问题。这是我正在使用的一些代码。

<cffile action="READ" file="c:\websites\db-utils\sample.xml" variable="xmlData">

<cfscript> 
    myxmldoc = XmlParse(xmlData); 
    modelNumber = XmlSearch(myxmldoc, "//basic_data/model_number"); 
    modelNumber = modelNumber[1].XmlText;

    enginename = XmlSearch(myxmldoc, "//engines/engine"); 
    enginename = enginename[1].XmlAttributes.name;

    camtype = XmlSearch(myxmldoc, "//engines/engine/cam_type"); 
    camtype = camtype[1].XmlText;

    transmissionname = XmlSearch(myxmldoc, "//transmissions/transmission"); 
    transmissionname = transmissionname[1].XmlAttributes.name;
</cfscript>
<cfoutput>
    Model Number: #modelNumber# <br />
    Engine: #enginename#<br>
    Cam: #camtype#<br>
    Trans: #transmissionname#<br>
</cfoutput>

我遇到的 XML 问题是当我变得更深并且需要遍历节点时。她是 XML 的一个片段。

<decoded_data>
    <decoder_messages></decoder_messages>
        <query_responses>
            <query_response identifier="1" transaction_id="CC969FDA9C2B546EEC0A8036F19C7A8543A7148E">
                <query_error></query_error>
                    <us_market_data>
                        <us_styles count="1">
                            <style name="XLE 4dr Sedan" vehicle_id="400892838" complete="Y" market="US Light-Duty" fleet="N">
                                <basic_data></basic_data>
                                <pricing></pricing>
                                <engines></engines>
                                <transmissions></transmissions>
                                <specifications>
                                    <category name="Drive Type"><specification name="Drive Type">FWD</specification></category>
                                    <category name="Fuel Tanks"><specification name="Fuel Tank 1 Capacity (Gallons)">17</specification></category>
                                    <category name="Interior Dimensions">
                                        <specification name="Cargo Volume">15.4</specification>
                                        <specification name="Passenger Volume">102.7</specification>
                                    </category>
                                    <category name="Measurements of Size and Shape">
                                        <specification name="Front Track Width">62.4</specification>
                                        <specification name="Ground Clearance">6.1</specification>
                                        <specification name="Height">57.9</specification>
                                        <specification name="Length">190.9</specification>
                                        <specification name="Rear Track Width">62</specification>
                                        <specification name="Wheelbase">109.3</specification>
                                        <specification name="Width">71.7</specification>
                                    </category>

这是我很难找到如何遍历规范及其中的类别的地方。同时获得“名称”和实际的“规范”。我有点用这段代码朝着正确的方向前进:

<cfset specNodes = xmlSearch(myxmldoc,'//specifications/category')>
<cfoutput>
<cfloop from="1" to="#arraylen(specNodes)#" index="i">
   <cfset specXML = xmlparse(specNodes[i])>
    #specXML#<br>   
</cfloop>
</cfoutput>

但我还不太清楚...任何帮助将不胜感激。

最佳答案

This is where is am having difficulty finding how to loop over the specifications and the categories within them.

你可以试试这个:

<!--- Get all category nodes under specifications. This will return an array --->
<cfset specNodes = xmlSearch(trim(xml), "//specifications/category")>

<!--- loop over array containing category nodes --->
<cfoutput>
  <cfloop from="1" to="#arrayLen(specNodes)#" index="catCounter">

    <!--- Get Name of the category --->
    Category Name : #specNodes[catCounter].xmlAttributes.name#<br />
    Specifications: <br />

    <!--- Loop over specifications of current category --->
    <cfloop from="1" to="#arrayLen(specNodes[catCounter].specification)#" index="specCounter">

        <!--- Get specification name --->
        Specification Name: #specNodes[catCounter].specification[specCounter].xmlAttributes.name#<br />

        <!--- Get specification value --->
        Specification Value: #specNodes[catCounter].specification[specCounter].xmlText#<br />
    </cfloop>
    <br /><br />
  </cfloop>
</cfoutput>

这将给出以下输出:

Category Name : Drive Type
Specifications: 
Specification Name: Drive Type
Specification Value: FWD


Category Name : Fuel Tanks
Specifications: 
Specification Name: Fuel Tank 1 Capacity (Gallons)
Specification Value: 17


Category Name : Interior Dimensions
Specifications: 
Specification Name: Cargo Volume
Specification Value: 15.4
Specification Name: Passenger Volume
Specification Value: 102.7


Category Name : Measurements of Size and Shape
Specifications: 
Specification Name: Front Track Width
Specification Value: 62.4
Specification Name: Ground Clearance
Specification Value: 6.1
Specification Name: Height
Specification Value: 57.9
Specification Name: Length
Specification Value: 190.9
Specification Name: Rear Track Width
Specification Value: 62
Specification Name: Wheelbase
Specification Value: 109.3
Specification Name: Width
Specification Value: 71.7

关于xml - 使用 Coldfusion 解析复杂的 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426119/

相关文章:

java.lang.IllegalArgumentException : local part cannot be "null" when creating a QName

java - 使用 Sax 解析具有相同标签的 XML 元素

xml - 使用 Groovy HTTPBuilder 发布 XML 数据

python - 如何将动态值传递给 xml 文件?

javascript - 将动态 jquery.qrcode RSS feed 与光滑 slider 集成

regex - 如何在 Lucee 中模拟 Unicode JS 正则表达式

Python元素树——从元素中提取文本,剥离标签

java - StringEscapeUtils.escapeXml 正在转换它不应该转换的 utf8 字符

mysql - 如何优化我的数据库?

http - cfhttp 在 CF9 上返回 415 不支持的媒体类型