I am parsing an XML file with vehicle data and specifications. I'm having trouble interleaving certain nodes to get multiple values. Here is the code that works for me.
<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>
I have a problem with XML when I get deeper and need to navigate through the nodes. It is an XML fragment.
<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>
That's where it’s hard for me to find how to iterate over specifications and categories inside them. Also get both the "name" and the actual "specification". I am going in the right direction with this code:
<cfset specNodes = xmlSearch(myxmldoc,'//specifications/category')>
<cfoutput>
<cfloop from="1" to="#arraylen(specNodes)#" index="i">
<cfset specXML = xmlparse(specNodes[i])>
#specXML#<br>
</cfloop>
</cfoutput>
But I'm not quite there ... any help would be appreciated.
source
share