我正在使用simpleXMLElement
创建一个新的 xml 文件。我想创建类似于以下内容的内容:
<?xml version="1.0"?>
<command type="scenario" name="test">
<game type="play" filename="google">
</game>
</command>
我正在使用此代码,但有些地方不正确:
<?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);
?>
但这是我在sad.xml
文件中看到的输出:
<?xml version="1.0"?>
<command type="scenario" name="test"><game type="play" filename="google"/></command>
为什么?
- 文件中没有出现新行
- 游戏标签没有
</game>
结束标签