Home > Code Snippets > Formatted XML, the php dom way – Best for debugging

Formatted XML, the php dom way – Best for debugging

Recently in a discussion on linkedin PHP webservice – Logging Requests/Responses, Roy de Kleijn had asked “how can I print the XML in XML structure, in stead of a single line of text”, which triggered me a thought. This may be trivial for veterans, and still a new information for newbies.

There is a script which I use heavily while debugging xml services, to check the responses from respective xml services. This gets worse when the response is in a mixed mode, where the java errors and exceptions are listed out as a stack trace. We might need to dissect the request xml to identify where we went wrong. The peice of testing interface is reproduced below.

header("Content-type: text/xml");
$dom = new DOMDocument();
$dom->formatOutput true;
?><form action="" method="post" target="gxml">
   <p><b>Raw Xml</b><br />
   <textarea style="width:100%;height: 50px;" name="xml"></textarea><br />
   <input type="submit" value="Get Formatted">
<iframe style="width:100%;height:500px" name="gxml"></iframe>

Now in a webservice gateway, if we want to log the response xml, we would use the following snippet at the top of the service file.

function logResponse($buffer)
$d = new DOMDocument();
$d->formatOutput true;

And in a client, or the webservice consuming system, just before the request is sent to the server, and just after the response is got from the server, we should log the xml incase we need to do the debugging.

Formatted XML, XML Formatting using DOM, Logging XML Services

Categories: Code Snippets Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

− one = three