$name = $_POST['sName'];
$ic = $_POST['sIC'];
$email = $_POST['sEmail'];
$address = $_POST['sAddress'];
$nophone = $_POST['sPhone'];
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
//loading the existing XML file
$dom->load('phone_XML.xml');
在同一个文件php中编写新的xml节点:
$phone = $dom->documentElement;
$staff = $dom->documentElement;
// appendChild() can be combined with the create*()
//$staff = $phone->appendChild($dom->createElement("staff"));
// you missed the `s` element node
$s = $staff->appendChild($dom->createElement('s'));
$s->setAttribute('id', 'S002Doe');
$s
->appendChild($dom->createElement('sName'))
->appendChild($dom->createTextNode($name));
$s
->appendChild($dom->createElement('sIC'))
->appendChild($dom->createTextNode($ic));
$s
->appendChild($dom->createElement('sEmail'))
->appendChild($dom->createTextNode($email));
$s
->appendChild($dom->createElement('sAddress'))
->appendChild($dom->createTextNode($address));
$s
->appendChild($dom->createElement('sPhone'))
->appendChild($dom->createTextNode($nophone));
保存在同一个文件中:
$dom->save('phone_XML.xml');
echo "<script language='JavaScript'>alert('Add item succesful');</script>";
echo "<script language='JavaScript'>window.location = 'index.php';</script>";
?>
输出应该是这样的:
<phone>
<staff>
<s id="S001Akmal">
<sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
<sIC>940228-10-6101</sIC>
<sEmail>muhdnurakmal@velocity.net.my</sEmail>
<sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
<sPhone>012-3456789</sPhone>
</s>
<s id="S002Atikah">
<sName> Nur Atikah Binti Rohizad </sName>
<sIC>940421-14-6236</sIC>
<sEmail>nuratikah@gmail.com</sEmail>
<sAddress>No 24, Taman Saujana Impian, 43000 Kajang</sAddress>
<sPhone>013-6752800 </sPhone>
</s>
</staff>
</phone>
答案 0 :(得分:0)
这里可能存在不同的问题,但具有相同的含义
祝你好运答案 1 :(得分:0)
您正在加载现有的XML。这将包括一些带有空格的文本节点(换行符,缩进空格)。 DOM在序列化中识别它们,试图在重新格式化时保留原始XML(包括空白节点)。 DOMDocument::$preserveWhiteSpace
属性允许它在解析时删除空白节点。
其他要点:
s
元素节点。 phone
元素节点是DOMDocument对象的属性中可用的文档元素。appendChild()
可与create*()
个电话结合使用。它返回附加节点。放在一起:
$xml = <<<'XML'
<phone>
<staff>
<s id="S001Akmal">
<sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
<sIC>940228-10-6101</sIC>
<sEmail>muhdnurakmal@velocity.net.my</sEmail>
<sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
<sPhone>012-3456789</sPhone>
</s>
</staff>
</phone>
XML;
$document = new DOMDocument();
$document->formatOutput = TRUE;
$document->preserveWhiteSpace = FALSE;
$document->loadXml($xml);
$phone = $document->documentElement;
// appendChild() can be combined with the create*()
$staff = $phone->appendChild($document->createElement("staff"));
// you missed the `s` element node
$s = $staff->appendChild($document->createElement('s'));
$s->setAttribute('id', 'S002Doe');
$s
->appendChild($document->createElement('sName'))
->appendChild($document->createTextNode("John Doe"));
$s
->appendChild($document->createElement('sIC'))
->appendChild($document->createTextNode("123"));
$s
->appendChild($document->createElement('sEmail'))
->appendChild($document->createTextNode("john@example.tld"));
$s
->appendChild($document->createElement('sAddress'))
->appendChild($document->createTextNode("somewhere"));
$s
->appendChild($document->createElement('sPhone'))
->appendChild($document->createTextNode("456"));
echo $document->saveXml();
输出:
<?xml version="1.0"?>
<phone>
<staff>
<s id="S001Akmal">
<sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
<sIC>940228-10-6101</sIC>
<sEmail>muhdnurakmal@velocity.net.my</sEmail>
<sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
<sPhone>012-3456789</sPhone>
</s>
</staff>
<staff>
<s id="S002Doe">
<sName>John Doe</sName>
<sIC>123</sIC>
<sEmail>john@example.tld</sEmail>
<sAddress>somewhere</sAddress>
<sPhone>456</sPhone>
</s>
</staff>
</phone>
提示:如果发布问题,请尝试构建可以在没有外部依赖性的情况下运行的完整示例。