将元素和节点添加到现有的xml中

时间:2014-04-21 00:27:00

标签: php xml

我有一个XML文件,其中包含以下内容。

<?xml version="1.0"?>
<users><user><firstname>ben</firstname><surname>kim</surname
<email>benkim@test.com</email></user>
<user><firstname>ken</firstname><surname>kim</surname><email>kenkim@test.com</email>   
</user></users>

我想做的是将新用户添加到上面的现有XML中,并使用以下代码。但我根本不工作。你们可以看看他们吗?

<?php
    header('Content-Type: text/xml');
?>
<?php
    $firstname = $_GET["firstname"];
    $surname = $_GET["surname"];
    $email = $_GET["email"];

    $person = array("firstname"=>$firstname,"surname"=>$surname,"email"=>$email);

    //ECHO (fromXML());

    $xmlDoc = new DOMDocument();
    $xmlDoc->load("test.xml");
    $xmlDoc=$xmlDoc->documentElement;

    toXml($person);

    function toXml($person)
    {
        $users = $xmlDoc->getElementsByTagName('users');

        $user = $xmlDoc->createElement('user');
        $user = $users->appendChild($user);

        $firstname = $xmlDoc->createElement('firstname'); 
        $firstname = $user->appendChild($firstname);   
        $valFirstname = $xmlDoc->createTextNode($person["firstname"]);
        $valFirstname = $firstname->appendChild($valFirstname);

        $surname = $xmlDoc->createElement('surname'); 
        $surname = $user->appendChild($surname);   
        $valSurname = $xmlDoc->createTextNode($person["surname"]);
        $valSurname = $surname->appendChild($valSurname);

        $email = $xmlDoc->createElement('email'); 
        $email = $user->appendChild($email);   
        $valEmail = $xmlDoc->createTextNode($person["email"]);
        $valEmail = $email->appendChild($valEmail);

        $strXml = $xmlDoc->saveXML();
        $handle = fopen("test.xml", "a");
        fwrite($handle, $strXml);
        fclose($handle);
    }

2 个答案:

答案 0 :(得分:0)

尝试使用SimpleXML代替

$xml = new SimpleXMLElement('your xml here');
$user = $xml->addChild('user');
$user->addChild('firstname', $person["firstname"]);

答案 1 :(得分:0)

您有多个问题。

首先,$xmlDoc在您的函数范围内不可用。为此,您需要:

  • 将$ xmlDoc作为toXml()函数的参数(建议使用)
  • 使用global关键字告诉您的函数需要从全局上下文中获取$ xmlDoc(不鼓励)

然后,您无法正确获取根节点。在您的情况下,您只需要使用$xmlDoc->firstChild获取它。

然后,您需要创建不同的节点,然后将它们插入根节点。这意味着您需要在将元素附加到DOM之前创建元素。

最后,您将以追加模式(test.xml)打开a文件。这意味着当您保存文档时,您实际上会将xml的内容附加到现有内容中,因此,您的文档将无效,因为每次执行时它都会有一个新的<?xml ... ?>标记脚本。

以下是您的脚本的更正版本。如果你有任何问题,请随时提问! :)

<?php

$person = array(
    "firstname" => $_GET["firstname"],
    "surname"   => $_GET["surname"],
    "email"     => $_GET["email"],
);

$xmlDoc = new DOMDocument();

$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
$xmlDoc->load("test.xml");

toXml($person, $xmlDoc);

header('Content-Type', 'application/xml');
exit(file_get_contents('test.xml'));

function toXml(array $person, DOMDocument $xmlDoc)
{
    $users = $xmlDoc->firstChild;
    $user = $xmlDoc->createElement('user');

    $firstname = $xmlDoc->createElement('firstname');
    $textNode = $xmlDoc->createTextNode($person["firstname"]);
    $firstname->appendChild($textNode);
    $user->appendChild($firstname);

    $surname = $xmlDoc->createElement('surname');
    $textNode = $xmlDoc->createTextNode($person["surname"]);
    $surname->appendChild($textNode);
    $user->appendChild($surname);

    $email = $xmlDoc->createElement('email');
    $textNode = $xmlDoc->createTextNode($person["email"]);
    $email->appendChild($textNode);
    $user->appendChild($email);

    $users->appendChild($user);

    $strXml = $xmlDoc->saveXML();
    $handle = fopen("test.xml", "w");
    fwrite($handle, $strXml);
    fclose($handle);
}