如何将节点添加到xml文件中:
<users>
<user uid="1" name="Alvin" email="example@example.com"/>
</users>
示例php:
$xml_file = "example.xml";
// WRITING TO XML FILE
if(!empty($_GET['add'])){
$u_name = stripslashes($_POST['u_name']);
$u_email = stripslashes($_POST['u_email']);
// Generate new (appended) ID
foreach($xml as $user){
$last_id = $user->uid;
}
$id = $last_id+1;
// Add node
$x = $xml->addChild("user");
$x->addChild("uid",$id);
$x->addChild("name",$u_name);
$x->addChild("email",$u_email);
$xml->asXML($xml_file);
}
感谢您的帮助和/或建议
答案 0 :(得分:1)
查看SimpleXML http://php.net/manual/en/book.simplexml.php
答案 1 :(得分:1)
请记住尝试Stackoverflow站点顶部的搜索功能。已经提交并回答了有关此主题的几个问题。
在Read and write an XML file with PHP
上查看上一个问题<强>更新强>: 以下是引用问题中接受的答案的摘录:
$pos = strpos($xml, "</videos>");
if ($pos === false) {
$xml = substr($xml,0,$pos)."\t<video url=\"$url\" desc=\"$desc\" />".substr($xml,$pos);
}