Skip navigation
Currently Being Moderated

webservices with xml file

Apr 14, 2012 7:56 AM

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

 
Replies
  • Currently Being Moderated
    Apr 15, 2012 3:43 AM   in reply to jfb00

    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.XmlTe xt>

                  </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#" >

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 12:03 AM   in reply to jfb00

    How does your cfargument line look like?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 1:35 PM   in reply to jfb00

    Odd. Just as a test, what happens when you drop the arguments scope, like this

     

    <cfloop from="1" to="#ArrayLen(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) & xmlObject.XmlRoot["root"].XmlChildren[arrIndx].author.XmlText>

                  </cfif>

    </cfloop

     
    |
    Mark as:
  • Currently Being Moderated
    May 4, 2012 1:49 AM   in reply to jfb00

    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></cfxm l>

        <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#" >

     
    |
    Mark as:
  • Currently Being Moderated
    May 5, 2012 7:15 AM   in reply to jfb00

    If the call to the web service continues to fail, then refresh the WSDL. The following code will refresh it:

     

    <cfset serviceFactory = CreateObject("java", "coldfusion.server.ServiceFactory")>

    <cfset rpcService = serviceFactory.XmlRpcService>

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

    <cfset rpcService.refreshWebService(wsdl)>

     

    Message was edited by: BKBK

     
    |
    Mark as:
  • Currently Being Moderated
    May 5, 2012 7:33 AM   in reply to jfb00

    jfb00 wrote:

     

    This solution work BKBK, Thanks!

    Now if it is not much to ask. I add a general node at the top of the xml like:

    <general>

        <accountUser>234</accountUser>

    </general>

     

    How can I go to the node "accountUser", change the value and return the whole xml file with this new value updated?

    <!--- The XML document object --->

    <cfxml variable="XMLObject"><root>

    <general>

        <accountUser>234</accountUser>

    </general>

    <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>

     

    <cfset XMLObject.root.general.accountUser.xmlText = 567>

     

    <cfdump var="#XMLObject#">

     
    |
    Mark as:
  • Currently Being Moderated
    May 5, 2012 10:29 AM   in reply to jfb00

    How kind of you!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points