Skip to main content
Inspiring
April 14, 2012
Answered

webservices with xml file

  • April 14, 2012
  • 1 reply
  • 3432 views

Hi,

How can I read xml file using web services?

Here is my cfc web services: (I just want to see how to read the xml file)

<cfcomponent>

  <cffunction name="getBooks" access="remote" returntype="string" output="no">

    <cfargument name="xmlObject" type="xml" required="yes" >

   

    <cfset BookResponse = "">

    <cfset var arrIndx = "">

    <cftry>

        <cfloop from="1" to="#ArrayLen(arguments.xmlObject.XmlChildren)#" index="arrIndx">

            <cfset BookResponse= arguments.xmlObject.XmlChildren[arrIndx].XmlName>

        </cfloop>

      <cfcatch type="any">

          <cfset BookResponse= "#cfcatch.message# #cfcatch.detail#">

     </cfcatch>

   </cftry>

<cfreturn BookResponse>

  </cffunction>

</cfcomponent>

Here my code to test the web services:

1. Define the xml

<!--- Setup some XML to work with --->

<cfsavecontent variable="XMLFile"><?xml version="1.0"?>

<root>

<header>

<user>1</user>

</header>

<book>

<isbn>0321330110</isbn>

<title>Macromedia ColdFusion MX 7 Certified Developer Study Guide</title>

<author>Ben Forta</author>

</book>

<header>

<user>2</user>

</header>

<book>

<isbn>0596004206</isbn>

<title>Learning XML, Second Edition</title>

<author>Erik Ray</author>

</book>

<header>

<user>3</user>

</header>

<book>

<isbn>0782140297</isbn>

<title>Coldfusion MX Developer's Handbook</title>

<author>Raymond Camden</author>

</book>

</root>

</cfsavecontent>

2.Invoke the web services

<cfinvoke

   method="getBooks"

   returnvariable="rawXMLBookList"

   webservice="http://localhost/Hoteleria/webServices/books.cfc?wsdl">

  

   <cfinvokeargument name="xmlObject" value="#XMLFile#">

</cfinvoke>

<!---<cfset XMLDocResult = XmlParse(rawXMLBookList)>

<cfdump var="#XMLDocResult#">--->

<cfdump var="#rawXMLBookList#' >

3. Error: Element XMLOBJECT.XMLCHILDREN is undefined in ARGUMENTS.

I try also using XMLParse without luck.

Any ideas?

Thanks

    This topic has been closed for replies.
    Correct answer BKBK

    I am back to this XML issue.

    I am getting this:

    Element XMLROOT is undefined in XMLOBJECT.

    Something is not clicking here.

    Any ideas?

    Thanks for your help!


    Sorry about that. It did work for me!

    What about doing it on the basis of strings, as follows:

    books.cfc

    <cfcomponent>

      <cffunction name="getAuthors" access="remote" returntype="string" output="yes">

        <cfargument name="xmlString" type="string" required="yes" >

       

       <cfset var xmlObject = "">

       <cfset var rootChildren = "">

       <cfset var authors = "">

       <cfset var arrIndx = "">

         

        <cfxml variable="xmlObject"><cfoutput>#arguments.xmlString#</cfoutput></cfxml>

        <cfset rootChildren = xmlObject.xmlRoot.xmlChildren>

       

        <cftry>

           

            <cfloop from="1" to="#ArrayLen(rootChildren)#" index="arrIndx">

                <!--- 'Author' elements are in 'book'. The 'book' elements are even-numbered --->

                <cfif arrIndx mod 2 is 0>

                    <cfset authors = authors & chr(10) & chr(13) & rootChildren[arrIndx].author.XmlText>

                  </cfif>

           </cfloop>

          <cfcatch type="any">

              <cfset authors = "#cfcatch.message# #cfcatch.detail#">

         </cfcatch>

       </cftry>

      <cfreturn authors>

      </cffunction>

    </cfcomponent>

    testPage.cfm


    <!--- Setup XML string to work with --->

    <cfsavecontent variable="XMLFile"><root>

    <header>

    <user>1</user>

    </header>

    <book>

    <isbn>0321330110</isbn>

    <title>Macromedia ColdFusion MX 7 Certified Developer Study Guide</title>

    <author>Ben Forta</author>

    </book>

    <header>

    <user>2</user>

    </header>

    <book>

    <isbn>0596004206</isbn>

    <title>Learning XML, Second Edition</title>

    <author>Erik Ray</author>

    </book>

    <header>

    <user>3</user>

    </header>

    <book>

    <isbn>0782140297</isbn>

    <title>Coldfusion MX Developer's Handbook</title>

    <author>Raymond Camden</author>

    </book>

    </root>

    </cfsavecontent>

    <!---Invoke the web services--->

    <cfinvoke

       method="getAuthors"

       returnvariable="authorList"

       webservice="http://localhost/Hoteleria/webServices/books.cfc?wsdl">

       <cfinvokeargument name="xmlString" value="#XMLFile#">

    </cfinvoke>

    <cfdump var="#authorList#" >

    1 reply

    jfb00Author
    Inspiring
    April 14, 2012

    Looks like is not passing the XML doc.

    I try this in my function: <cfset BookResponse = #xmlObject#>

    Just to see if I can see the XML, I am getting this:  [#document: null]

    Any ideas?

    jfb00Author
    Inspiring
    April 14, 2012
    BKBK
    Community Expert
    Community Expert
    April 15, 2012

    There are few things you can improve, for example, using cfxml to pass an XML object, rather than cfsavecontent which passes a string. The following ilustrates this and other points:

    books.cfc

    <cfcomponent>

      <cffunction name="getAuthors" access="remote" returntype="string" output="no">

        <cfargument name="xmlObject" type="xml" required="yes" >

      

        <cfset var authors = "">

        <cfset var arrIndx = "">

        <cftry>

            <cfloop from="1" to="#ArrayLen(arguments.xmlObject.XmlRoot["root"].XmlChildren)#" index="arrIndx">

                <!--- 'Author' elements are in 'book'. The 'book' elements are even-numbered --->

                <cfif arrIndx mod 2 is 0>

                    <cfset authors = authors & chr(10) & chr(13) & arguments.xmlObject.XmlRoot["root"].XmlChildren[arrIndx].author.XmlText>

                  </cfif>

           </cfloop>

          <cfcatch type="any">

              <cfset authors = "#cfcatch.message# #cfcatch.detail#">

         </cfcatch>

       </cftry>

      <cfreturn authors>

      </cffunction>

    </cfcomponent>

    testPage.cfm

    <!--- Setup some XML to work with --->

    <cfxml variable="XMLFile"><root>

    <header>

    <user>1</user>

    </header>

    <book>

    <isbn>0321330110</isbn>

    <title>Macromedia ColdFusion MX 7 Certified Developer Study Guide</title>

    <author>Ben Forta</author>

    </book>

    <header>

    <user>2</user>

    </header>

    <book>

    <isbn>0596004206</isbn>

    <title>Learning XML, Second Edition</title>

    <author>Erik Ray</author>

    </book>

    <header>

    <user>3</user>

    </header>

    <book>

    <isbn>0782140297</isbn>

    <title>Coldfusion MX Developer's Handbook</title>

    <author>Raymond Camden</author>

    </book>

    </root>

    </cfxml>

    <!---Invoke the web services--->

    <cfinvoke

       method="getAuthors"

       returnvariable="authorList"

       webservice="http://localhost/Hoteleria/webServices/books.cfc?wsdl">

       <cfinvokeargument name="xmlObject" value="#XMLFile#">

    </cfinvoke>

    <cfdump var="#authorList#" >