tags:

views:

26

answers:

1

I want to return the following XML in SOAP when a function is called rather than just a string. There are many different functions that will return an xml formatted this way, is there a way that I can define what a function returns? Like making my own data type?

<meta>
    <supplier>Amazon.com</supplier>
    <datetime>2010/08/21 14:32:40</datetime>
</meta>
<results>
    <result>
        <manufacturer>Intel</manufacturer>
        <itemno>8236476234</itemno>
        <prices>
            <price>
                <quantity>10</quantity>
                <cost>$1.20</cost>
            </price>
            <price>
                <quantity>100</quantity>
                <cost>$0.80</cost>
            </price>
        </prices>
    </result>
    <result> ... </result>
</results>
A: 

The best way is to put your result in an array and then convert it to XML. Here is a ready made script for that:

http://rssphp.net/examples/convert_array_to_xml/

It converts almost any kind of associative array to full formed XML.

shamittomar
XML cannot be converted into a simple associative array like that because valid XML can have element attributes. The best way to manipulate an XML document is using something like DOM or SAX, which are tried and true solutions. But if your aim is simply to store/transmit XML data, then the XML format itself is already a perfectly viable and standardized serialization format. There's no benefit to converting it into some other data structure.
Lèse majesté