PHP - 使用 DOM 编辑 XML

标签 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 在末尾有两个结束文章标记。

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

感谢大家的阅读

最佳答案

由于您在原始帖子中出现了该文章/文章错误,是否可能是您更改了适用于类似文档的现有代码示例

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

关于PHP - 使用 DOM 编辑 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27782628/

相关文章:

php - 将特殊字符 ⓄⒼקร 写入/检索到数据库

sql - 如何根据XSD从数据库导出数据到xml

python - xml解析和 latex 符号

javascript - 为什么这个 jquery 不起作用?

php - 如何从谷歌翻译 api 中删除或隐藏谷歌图标?

php - MySQL 事务在 PHPMyAdmin 中运行时有效,但在从 PHP 文件运行时无效

php - 真/假在 PHP 中是如何工作的?

java - Android XML Reader 读取一次

javascript - getElementById 在 Safari 中返回 null 或 undefined

javascript - 如何检查选择框中是否存在值