PHP - 使用DOM编辑XML

时间:2015-01-05 15:27:50

标签: php xml 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最后会有两个最终文章标记。

有人可以帮我找到正确的方法吗?如果你找一点时间来解释我的错误,那就太棒了。

谢谢大家阅读

2 个答案:

答案 0 :(得分:2)

由于您的原始帖子中有文章/文章错误,您是否已更改了处理文档的现有代码示例,例如

<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>

答案 1 :(得分:1)

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

$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创建新元素并以root身份插入

// 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();
?>