• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Parsing XML

Community Beginner ,
Sep 05, 2006 Sep 05, 2006

Copy link to clipboard

Copied

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


TOPICS
Advanced techniques

Views

442

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Beginner , Sep 06, 2006 Sep 06, 2006
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

Votes

Translate

Translate
Advisor ,
Sep 05, 2006 Sep 05, 2006

Copy link to clipboard

Copied

If you save your XML as a file called "ForumCart.xml" then the attached code should get you started...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 06, 2006 Sep 06, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Sep 06, 2006 Sep 06, 2006

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 06, 2006 Sep 06, 2006

Copy link to clipboard

Copied

LATEST
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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 06, 2006 Sep 06, 2006

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 06, 2006 Sep 06, 2006

Copy link to clipboard

Copied

Thank you Ian. Question, how do I get the children to only loop once? It keeps repeating itself about 12 times.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 06, 2006 Sep 06, 2006

Copy link to clipboard

Copied

In my version:

<cfoutput>
<cfif
XMLobj.orderrequest.lineitems.xmlChildren .xmlChildren.xmlName
NEQ "description">
#XMLobj.orderrequest.lineitems.xmlChildren
.xmlChildren.xmlName#
#XMLobj.orderrequest.lineitems.xmlChildren .xmlChildren.xmlText#
</cfoutput>
</cfoutput>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation