Skip to main content
Participant
September 6, 2006
Answered

Parsing XML

  • September 6, 2006
  • 2 replies
  • 527 views
I have the following XML file that I am trying to parse but I am having some trouble parsing the children nodes (is that what they are called?). I can't seem to find the proper syntax to parse XML children. For example, I want to output all the children nodes of lineitem1(i.e. skunumber, weight, color, clarity, cut, clarification.) and lineitem2 (i.e. skunumber, metal, style, size) and combine them all on one line. I know this is possible and probably very easy. I am looking for a hint on syntax with using my XML file below and then I can probably take it from there.

Also, in lineitem1 and lineitem2 there are fields called unitprice. They both have a value. What is the best way to add these two fields together and output just that total number?

I appreciate all help and responses.

Thanks,

Josh


This topic has been closed for replies.
Correct answer jfilan
quote:

Originally posted by: jfilan
Holy crap! Thank you so much. I see that all children data is getting parsed. How would I exclude the <description> node but keep everything else? Again, thank you so much. This is such a great starting point for me.

Josh


At the top of the source add this line:
<CFSET sPropertiesToIgnore = "description,polish">

Then in the innermost <cfloop> you would use something like:
<CFIF 0 EQ ListFindNoCase (sPropertiesToIgnore, sPropName)>
<CFOUTPUT> <b><i>#sPropName#:</i></b> #sPropVal#   </CFOUTPUT>
<CFELSE>
<!--- Here we Ignored property "#sPropName#". --->
</CFIF>


At the top of the source add this line:
<CFSET sPropertiesToIgnore = "description,polish">

Then in the innermost <cfloop> you would use something like:
<CFIF 0 EQ ListFindNoCase (sPropertiesToIgnore, sPropName)>
<CFOUTPUT> <b><i>#sPropName#:</i></b> #sPropVal#   </CFOUTPUT>
<CFELSE>
<!--- Here we Ignored property "#sPropName#". --->
</CFIF>




Thank you almighty ColdFusion god.

Josh

2 replies

Inspiring
September 6, 2006
Another way to skim this XML cat.

XMLobj = your XML data as a CF xmlOjb.

<cfloop
from="1"
to="#arrayLen(XMLobj.orderrequest.lineitems.xmlChildren)#"
index="i">
<cfloop
from="1"

to="#arrayLen(XMLobj.orderrequest.lineitems.xmlChildren .xmlChildren)#"
index="j">
<cfoutput>

#XMLobj.orderrequest.lineitems.xmlChildren
.xmlChildren.xmlName#

#XMLobj.orderrequest.lineitems.xmlChildren .xmlChildren.xmlText#
</cfoutput>
</cfloop>
</cfloop>

Someways you can create xmlObj.

<cfile action="read" file="file.path.xmldoc.xml" varible="rawXml">
<cfset xmlObj = xmlParse(rawXml)>

OR

<cfxml variable="xmlObj">
<orderrequest version="1.0" orderID="THX1138" issuedate="20060802"
lang="en-us">
<customer number="3263827">
<fname>Ezra</fname>
<lname>Pound</lname>
...
</orderrequest>
</cfxml>

OR

<cfsavecontent variable="rawXML">
<orderrequest version="1.0" orderID="THX1138" issuedate="20060802"
lang="en-us">
<customer number="3263827">
<fname>Ezra</fname>
<lname>Pound</lname>
...
</orderrequest>
</cfxml>
</cfsavecontent>
<cfset xmlObj = xmlParse(rawXML)>

OR

<cfhttp url=" http://#CGI.SERVER_NAME##sWebFolder#/ForumCart.xml"
method="get">

<CFSET sXML_Raw = CFHTTP.FileContent>
<CFSET zXML_Data = XMLParse (Trim (sXML_Raw))>


PS
None of this code is tested or debugged. Expect to have to correct some
syntax or the other.
jfilanAuthor
Participant
September 6, 2006
Thank you Ian. Question, how do I get the children to only loop once? It keeps repeating itself about 12 times.
September 6, 2006
If you save your XML as a file called "ForumCart.xml" then the attached code should get you started...

jfilanAuthor
Participant
September 6, 2006
Holy crap! Thank you so much. I see that all children data is getting parsed. How would I exclude the <description> node but keep everything else? Again, thank you so much. This is such a great starting point for me.

Josh
September 6, 2006
quote:

Originally posted by: jfilan
Holy crap! Thank you so much. I see that all children data is getting parsed. How would I exclude the <description> node but keep everything else? Again, thank you so much. This is such a great starting point for me.

Josh


At the top of the source add this line:
<CFSET sPropertiesToIgnore = "description,polish">

Then in the innermost <cfloop> you would use something like:
<CFIF 0 EQ ListFindNoCase (sPropertiesToIgnore, sPropName)>
<CFOUTPUT> <b><i>#sPropName#:</i></b> #sPropVal#   </CFOUTPUT>
<CFELSE>
<!--- Here we Ignored property "#sPropName#". --->
</CFIF>