views:

335

answers:

2

Hi,

I am trying to generate the following XML in my SOAP call:

<CResponse>
    <ID>int</ID>
    <Response Action="Apply">
        <Question ID="$someint">
            <Responses>
                <Response ID="$someotherint" />
                <Response ID="$yetanotherint" />
            </Responses>
        </Question>
    </Response>
</CResponse>

I can create most of the call just fine - I've finally picked up that nested arrays are my friends - but I have no idea how to add those ID="$int" and Action="Apply" attributes to the various tags. I'm sure this is fairly easy, but I just can't figure it out.

TIA.

A: 

You should be able to add attributes with following syntax:

array("foo" => array("_" => "cheese", "bar"=>"moo"));

This should produce following XML

<foo bar="moo">cheese</foo>
JJ
+1  A: 

I don't know about the (somewhat weird) syntax suggested by JJ (and according to your reposting of the question, it doesn't seem to work), but you should be able to shape your request xml anyway you want by constructing it more or less 'manually' using the SoapVar class with the XSD_ANYXML encoding.

See this user comment to the SoapVar Constructor documentation for an example of how to do this.

If the XML you need to construct gets more complex, consider using SimpleXML or DOMDocument to assemble it instead of writing it directly.

(It still seems odd to me that there should be no simpler way to do this, but so far I have not found one.)


Edit: Just found another example using the XSD_ANYXML encoding in this question asking for a simpler way to do it. Unfortunately no one came up with a simpler way so far :/

Henrik Opel