tags:

views:

79

answers:

3

Hello, is there a way to reduce the number of bytes sent to the network from a SOAP response? I use nuSoap in PHP and would like to avoid xsi:type="xsd:string", xsi:type="xsd:int" in every node. Is there a way to do it?

Thanks in advance c.

A: 

The usual way to do this is to enable compression in the HTTP layer. If you want to make sure that you're always compressing data, you could even reject SOAP requests that are not compressed. (A popular software Q&A site does this.)

Greg Hewgill
the problem is that who reads xml does not want "useless" tags: if i enable compression i do not avoid this, is right? I would need to send simple structures like <TAG1><TAG2>123</TAG2></TAG1> instead of <SOAP:bla><TAG1 xsi:type="xsd:string"><TAG2 xsi:type="xsd:string">123</TAG2></TAG1></SOAP:bla>
Cris
+1  A: 

If you are strictly tied to sending valid SOAP response back to Flash client, and if using valid schema is not an option (which would simplify output markup), then no, you can't do anything about it...

However, if you're NOT tied to sending valid SOAP response, and your developer really insists on getting non-verbose markup, then you are left with an alternative of rolling your own. If this is the case, read on.

You can use some of existing XML Serializer classes available via PEAR, or some other source. If you wanted to go down the Trully-Roll-Your-Own route, there are PHP classes to aid you in creating valid XML directly.

Not to repeat what's being said elsewhere, here's in-depth example about how to serialize some data using PEAR XML_Serializer.

Another really simple example, this time it's SimpleXML-based, where you add child elements and attributes as needed to meet desired output.

$xml = new SimpleXMLElement('<?xml version="1.0" standalone="yes"?>');
$ch1 = $xml->addChild("root");
$ch2 = $ch1->addChild("element");
// you obviously have no use for attributes, but I included it for completeness
$ch2->addAttribute("foo", "bar");
$ch2->addChild("subElement", "value1");

$outxml = $xml->asXML();

Code above would produce something like:

<?xml version="1.0" standalone="yes"?>
<root>
    <element foo="bar">
         <subElement>value1</subElement>
    </element>
</root>

I hope it helps.

mr.b
A: 

Remove whitespaces and gzip the contents during transport.

header('Content-Encoding: gzip');
$xml=new DOMDocument($soap);
$xml->preserveWhiteSpace(FALSE);
echo $xml->saveXML();
stillstanding