xml new line when adding a child

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

xml new line when adding a child

Post by susrisha »

Code: Select all

 
<?xml version="1.0"?>
<versionchecker result="error" code="1"><description> App version not given</description></versionchecker>
 
To create the above xml as file, i have the following code in a function

Code: Select all

 
$response = simplexml_load_string('<versionchecker></versionchecker>');
  $response->addAttribute('result',"error");
 // $response->addChild('errorcode',"$errorno");
  $response->addAttribute('code',"$errorno");
  
  $response->addChild('description',"$errordesc");
 
Now the problem is that i want the output like this..

Code: Select all

 
<?xml version="1.0"?>
<versionchecker result="error" code="1">
<description> App version not given</description>
</versionchecker>
 
That is... i want to add every child in a new line.. how do i do it??
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: xml new line when adding a child

Post by requinix »

SimpleXML doesn't have a way to format output, but DOMDocument does.

Create the xml data, feed it to a DOMDocument, set the $formatOutput flag to true, and call saveXML().
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: xml new line when adding a child

Post by susrisha »

yaa i got that from http://in.php.net/manual/en/function.si ... -asXML.php

Code: Select all

 
 
$doc = new DOMDocument('1.0');
  $doc->formatOutput = true;
  $domnode = dom_import_simplexml($response);
  $domnode = $doc->importNode($domnode, true);
  $domnode = $doc->appendChild($domnode);
  return $doc->saveXML();
 
Post Reply