4 Replies Latest reply on Oct 27, 2012 2:41 AM by BKBK

    Converting this simple XML to a Struct?

    jcelias

      Hi everyone,

       

      I'm having trouble converting this XML to a Struct.

       

      <book_ids quantity="2">7,9</book_ids>

       

      I'm supposed to pass that as a parameter to a remote Web Service using cfinvoke. Can anyone help out on how to convert that XML to a Coldfusion Struct???

       

      Thanks in advance for any help!

       

      JC

        • 1. Re: Converting this simple XML to a Struct?
          Carl Von Stetten Adobe Community Professional & MVP

          What is the Web Service expecting as a parameter?  More information is needed.

          -Carl V.

          • 2. Re: Converting this simple XML to a Struct?
            jcelias Level 1

            Hi Carl,

             

            Here's the relevant part of the WSDL:

             

            <s:element name="getBooks">

             

            <s:complexType>

             

             

            <s:sequence>

             

             

            <s:element minOccurs="0" maxOccurs="1" name="book_ids" type="tns:BookIDsCollection"/>

             

            </s:sequence>

            </s:complexType>

            </s:element>

             

            <s:complexType name="BookIDsCollection">

             

             

            <s:simpleContent>

             

             

            <s:extension base="s:string">

             

             

            <s:attribute name="quantity" type="s:int" use="required"/>

             

            </s:extension>

            </s:simpleContent>

            </s:complexType>

            • 3. Re: Converting this simple XML to a Struct?
              Carl Von Stetten Adobe Community Professional & MVP

              While I don't have any insight on this (I don't work with web services much), the additional information should make it easier for others to assist you.

              -Carl V.

              • 4. Re: Converting this simple XML to a Struct?
                BKBK Adobe Community Professional & MVP

                You cannot, in general, convert XML into struct. An XML object has elements, which may or may not have attributes. The attributes have values and the element itself may have a "value" in the form of XMLText. Whereas, a struct basically has key-value pairs. As XML packs many more properties that a struct, a one-to-one mapping is often impossible.

                 

                In any case, there is little point converting the XML to a struct. A struct is typical ColdFusion lingo that wont be understood by webservices. I would suggest you send the XML itself to the service! How? As a SOAP message.

                 

                You could do something like this

                 

                !--- Web service WSDL --->

                <cfset wsdl_url="http://mydomain.com/myws.cfc?wsdl">

                       

                <!--- Compose SOAP message to send to web service--->

                <cfsavecontent variable="soap"><?xml version="1.0" encoding="UTF-8" ?>

                <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

                soapenv:Body>               

                <book_ids quantity="2">7,9</book_ids>               

                </soapenv:Body>

                </soapenv:Envelope>

                </cfsavecontent>

                 

                <!--- Invoke web service and send message--->

                <cftry>    

                        <cfhttp url="#wsdl_url#" method="post" >

                            <cfhttpparam type="header" name="content-type" value="text/xml">

                            <cfhttpparam type="header" name="SOAPAction" value="">

                            <cfhttpparam type="header" name="content-length" value="#len(soap)#">

                            <cfhttpparam type="header" name="charset" value="utf-8">

                            <cfhttpparam type="xml" name="message" value="#trim(soap)#">

                        </cfhttp>

                        <p><cfoutput>#xmlFormat(cfhttp.fileContent)#</cfoutput> </p>

                      

                    <cfcatch type="any">

                        <cfdump var="#cfcatch#">

                    </cfcatch>

                </cftry>