7 Replies Latest reply on Sep 6, 2006 10:51 AM by jfilan

    Parsing XML

    jfilan
      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


        • 1. Re: Parsing XML
          MikerRoo Level 1
          If you save your XML as a file called "ForumCart.xml" then the attached code should get you started...

          • 2. Re: Parsing XML
            Level 7
            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[j].xmlName#

            #XMLobj.orderrequest.lineitems.xmlChildren .xmlChildren[j].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.
            • 3. Re: Parsing XML
              jfilan Level 1
              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
              • 4. Re: Parsing XML
                jfilan Level 1
                Thank you Ian. Question, how do I get the children to only loop once? It keeps repeating itself about 12 times.
                • 5. Re: Parsing XML
                  MikerRoo Level 1
                  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>

                  • 6. Re: Parsing XML
                    Level 7
                    In my version:

                    <cfoutput>
                    <cfif
                    XMLobj.orderrequest.lineitems.xmlChildren .xmlChildren[j].xmlName
                    NEQ "description">
                    #XMLobj.orderrequest.lineitems.xmlChildren
                    .xmlChildren[j].xmlName#
                    #XMLobj.orderrequest.lineitems.xmlChildren .xmlChildren[j].xmlText#
                    </cfoutput>
                    </cfoutput>
                    • 7. Re: Parsing XML
                      jfilan Level 1
                      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