PHP-使用DOM编辑XML

PHP-使用DOM编辑XML,php,xml,dom,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> $dom=new DOMDocument(); $dom->load("

我使用的是创建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>
$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>

2015年01月05日04:32:36约翰·斯图尔特
2015年1月6日12:00:00伊万·迪莫夫
所以一般来说,它在结束旧的前添加了新的
,现在XML在末尾有两个end-article标记

有人能帮我找到正确的方法吗?如果你能抽出一点时间来解释我出了什么问题,那就太棒了


感谢大家阅读

您将获得一个嵌套元素,而您只需要在第一个元素的相同级别上创建一个新元素。所以只要使用

$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.net中的第一个完整示例中:示例#1创建新元素并将其作为根插入

//示例#1创建新元素并将其作为根插入

既然您在最初的帖子中出现了那篇文章/文章错误,是否您已经更改了一个用于以下文档的现有代码示例

<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;
}
documentElement->appendChild($article);
echo$doc->savexml();
函数newArticle(DOMDocument$doc)
{
$article=$doc->createElement(“article”);
$article->setAttribute(“id”,2);
$published=$doc->CreateElement(“published”);
$publishedDate=$doc->createTextNode(日期(“d/m/Y h:i:s”,标准时间(“明天”));
$published->appendChild($publishedDate);
$article->appendChild($published);
$author=$doc->createElement(“作者”);
$author->setAttribute(“位置”、“编写器”);
$authorName=$doc->createTextNode(“Ivan Dimov”);
$author->appendChild($authorName);
$article->appendChild($author);
退回$article;
}
函数getData(){

返回说明您在末尾有
article
articleS
标记。第二个标记后面有一个
s
。非常好的一点@fedorqui,无论如何,现在我在编辑之前在XML文件中更改了它。但是我仍然需要更改PHP编辑XML的方式,因为现在我在末尾有两个标记。您是对的!谢谢您的朋友。我会将这标记为请尽快回答我的问题。。但是现在您有一个包含多个根/顶级元素的XML文档。@VolkerK是的,不是一个非常规范的XML,但它显然是OP想要的。@Andurit啊,现在我看到您在另一个答案中还提出了其他问题。不,请继续并接受对您帮助最大的答案。要很遗憾,我以前没有看到它。以后,试着在主要问题中提问,而不是在评论中提问。现在我让它工作了,它看起来,不确定它是否100%正确,适用于代码1和安全性等诸如此类的东西,但看起来它是一个初步工作。OP仍然可能希望在a/an中以片段/实体的形式包含文章列表其他xml文档-使附加元素更容易。在这种情况下,文章元素可以是“顶级”序列。但是xml声明必须被删除。看起来我对xml知之甚少,知道@VolkerK很酷,实际上也是一个完美的例子,但不知怎的,我不能让它保存,只打印。(我在保存之前删除了那个“echo”)你能不能多花点时间来帮助我让你的完美例子为我工作?谢谢你已经从我这里获得了经验。你必须回到->保存()而不是->保存XML()。与loadxml()/load()…oO一样(如果load()/save()采用流参数而不是单独的loadxml()/savexml()方法岂不是很酷?)文档中不应该有多个顶级元素节点,这就是文档片段的用途。
<articles>
  <article id="1">...</article>
...
  <article id="n">...</article>
</articles>
<?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>