我有我的代码工作,输出基于HTML表单上的XML文件,但输出格式仅仅是一个长的字符串是这样的:格式化XML输出文件
<?xml version="1.0"?>
<students>
<student><name>Joey Lowery</name><email>[email protected]</email><cell>555-555-5555</cell><dob>1999-03-31</dob><study>8</study></student></students>
,而不是这样的:
<?xml version="1.0"?>
<students>
<student>
<name>Joey Lowery</name>
<email>[email protected]</email>
<cell>555-555-5555</cell>
<dob>1999-03-31</dob>
<study>8</study>
</student>
</students>
我使用formatOutput = true
以及preserveWhiteSpace = false
,但它不起作用。以下是我的代码:
if(isset($_POST['submit'])) {
$file = "data.xml";
$userNode = 'student';
$doc = new DOMDocument('1.0');
$doc->load($file);
$doc->preserveWhiteSpace = true;
$doc->formatOutput = true;
$root = $doc->documentElement;
$post = $_POST;
unset($post['submit']);
$user = $doc->createElement($userNode);
$user = $root->appendChild($user);
foreach ($post as $key => $value) {
$node = $doc->createElement($key, $value);
$user->appendChild($node);
}
$doc->save($file) or die("Error");
header('Location: thanks.php');
}
检查此答案:http://stackoverflow.com/questions/3616540/format-xml-string/3616722#3616722。 –
我试图将它保存到一个文件,并且该答案似乎不适用。尽管如此,感谢您的尝试。 –