I am using simpleXMLElement
to create a new xml file.
I want to create something similar to the following:
<?xml version="1.0"?>
<command type="scenario" name="test">
<game type="play" filename="google">
</game>
</command>
I am using this code but something is not correct:
<?php
$xml = new SimpleXMLElement("<?xml version=\"1.0\" ?><command type='scenario'></command>");
$xml->addAttribute('name', 'test');
$game = $xml->addChild('game');
$game->addAttribute('type', 'play');
$game->addAttribute('filename', 'google');
// checking the output
$handler = fopen(sad.xml, 'w');
fwrite($handler, $xml->asXML());
fclose($handler);
?>
But this is the output i see in sad.xml
file instead:
<?xml version="1.0"?>
<command type="scenario" name="test"><game type="play" filename="google"/></command>
Why?
- New lines are not appearing in the file
- game tag does not have
</game>
closing tag