我正在使用车辆数据和规范解析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>
但我不在那里......任何帮助都会受到赞赏。
答案 0 :(得分:2)
这是我很难找到如何循环的 规范及其中的类别。
你可以试试这个:
<!--- 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
答案 1 :(得分:0)
你可以这样做:
<cfloop from="1" to="#arraylen(specNodes)#" index="i">
<h3>#specNodes[i].XmlAttributes.name#</h3>
<cfset specChildren = specNodes[i].specification />
<cfloop from="1" to="#arraylen(specChildren)#" index="j">
<br/>#specChildren[j].XmlAttributes.name# = #specChildren[j].XmlText#
</cfloop>
</cfloop>
这给了你:
**Drive Type**
Drive Type = FWD
**Fuel Tanks**
Fuel Tank 1 Capacity (Gallons) = 17
**Interior Dimensions**
Cargo Volume = 15.4
Passenger Volume = 102.7
**Measurements of Size and Shape**
Front Track Width = 62.4
Ground Clearance = 6.1
Height = 57.9
Length = 190.9
Rear Track Width = 62
Wheelbase = 109.3
Width = 71.7