【问题标题】:adding nodes to xml [duplicate]将节点添加到 xml [重复]
【发布时间】:2012-03-14 17:37:02
【问题描述】:

可能重复:
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);
    }

感谢您的任何帮助和/或建议

【问题讨论】:

标签: php xml


【解决方案1】:

【讨论】:

    【解决方案2】:

    记得试试 Stackoverflow 网站顶部的搜索功能。已经提交并回答了几个关于这个主题的问题。

    查看Read and write an XML file with PHP上的这个问题

    更新: 以下是引用问题中已接受答案的 sn-p:

    $pos = strpos($xml, "</videos>");
    
    if ($pos === false) {
        $xml = substr($xml,0,$pos)."\t<video url=\"$url\" desc=\"$desc\" />".substr($xml,$pos);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-08
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多