将节点添加到xml

时间:2012-03-14 17:37:02

标签: php xml

  

可能重复:
  How is possible add child to the specific node

如何将节点添加到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);
    }

感谢您的帮助和/或建议

2 个答案:

答案 0 :(得分:1)

答案 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);
}