PHP - 使用 DOM 编辑 XML

PHP - Edit XML using DOM

我正在使用创建 XML.

的简单 PHP 脚本

XML 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<article id="1"><published>05/01/2015 04:32:36</published><author position="writer">John Stewart</author>

没什么特别的吧?是的,之后我写了另一个 PHP 脚本,试图将第二篇文章添加到 XML。

代码如下:

$dom=new DOMDocument();

$dom->load("articles.xml");

$article=$dom->createElement("article");
$article->setAttribute("id", 2);
$published=$dom->CreateElement("published");
$publishedDate=$dom->createTextNode(date("d/m/Y h:i:s", strtotime("tomorrow")));
$published->appendChild($publishedDate);
$article->appendChild($published);
$author=$dom->createElement("author");
$author->setAttribute("position", "writer");
$authorName=$dom->createTextNode("Ivan Dimov");
$author->appendChild($authorName);
$article->appendChild($author);
$dom->documentElement->appendChild($article);

$dom->save("articles.xml");

我可能是瞎了,因为这段代码对我来说看起来不错,但是生成 XML 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<article id="1"><published>05/01/2015 04:32:36</published><author position="writer">John Stewart</author>
<article id="2"><published>06/01/2015 12:00:00</published><author position="writer">Ivan Dimov</author></article></article>

所以一般来说,它会在结束旧的之前添加新的 <article>,现在 XML 在末尾有两次结束文章标签。

有人可以帮我找到正确的方法吗?如果您能抽出一点时间向我解释哪里出了问题,那就太棒了。

感谢大家阅读

您得到的是一个嵌套元素,而您只需要一个与第一个元素处于同一级别的新元素。所以只需使用

$dom->appendChild($article);

而不是

$dom->documentElement->appendChild($article);

我用你的脚本试过了,我得到:

<?xml version="1.0" encoding="UTF-8"?>
<articles id="1"><published>05/01/2015 04:23:18</published><author position="writer">John Stewart</author></articles>
<articles id="2"><published>06/01/2015 12:00:00</published><author position="writer">Ivan Dimov</author></articles>

所以完整的代码是:

<?php

$dom=new DOMDocument();
$dom->load("articles.xml");

$article=$dom->createElement("articles");
...
$article->appendChild($author);
//$dom->documentElement->appendChild($article); // commented
$dom->appendChild($article);                    // the good one

$dom->save("articles.xml");

信息取自 php.net 中的 DOMDocument::createElement 第一个完整示例:示例 #1 创建一个新元素并将其作为根插入

// Example #1 Creating a new element and inserting it as root
<?php

$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElement('test', 'This is the root element!');

// We insert the new element as root (child of the document)
$dom->appendChild($element);

echo $dom->saveXML();
?>

由于您在原始 post 中存在 article/articles 错误,是否可能是您更改了适用于

等文档的现有代码示例
<articles>
  <article id="1">...</article>
...
  <article id="n">...</article>
</articles>

?
根据 xml 规范,一个 xml 文档只有一个文档元素,不能更多。

<?php
$doc=new DOMDocument();
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
$doc->loadxml( getData() );
$article = newArticle($doc);
$doc->documentElement->appendChild($article);
echo $doc->savexml();



function newArticle(DOMDocument $doc)
{
    $article=$doc->createElement("article");
    $article->setAttribute("id", 2);
    $published=$doc->CreateElement("published");
    $publishedDate=$doc->createTextNode(date("d/m/Y h:i:s", strtotime("tomorrow")));
    $published->appendChild($publishedDate);
    $article->appendChild($published);
    $author=$doc->createElement("author");
    $author->setAttribute("position", "writer");
    $authorName=$doc->createTextNode("Ivan Dimov");
    $author->appendChild($authorName);
    $article->appendChild($author);
    return $article;
}

function getData() {
return <<< eox
<?xml version="1.0" encoding="UTF-8"?>
<articles>
    <article id="1">
        <published>05/01/2015 04:32:36</published>
        <author position="writer">John Stewart</author>
    </article>
</articles>
eox;
}

打印

<?xml version="1.0" encoding="UTF-8"?>
<articles>
  <article id="1">
    <published>05/01/2015 04:32:36</published>
    <author position="writer">John Stewart</author>
  </article>
  <article id="2">
    <published>06/01/2015 12:00:00</published>
    <author position="writer">Ivan Dimov</author>
  </article>
</articles>