简体   繁体   中英

PHP - Edit XML using DOM

I am using simple PHP script which creates XML.

XML looks like:

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

Nothing special right? Yea and after that i write another PHP script which try to add second article to XML.

The code looks like:

$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");

I'm probably blind because this code looks good for me, but generate XML looks like this:

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

So in general, it adds new <article> before it end old one , and now XML have twice end article tag at the end.

Can someone help me to find the correct way how to do that? If you find a bit time to explain me what was wrong it would be awesome.

Thank you all for reading

Since you had that article/articles error in your original post, could it be that you've changed an existing code example that worked on a document like

<articles>
  <article id="1">...</article>
...
  <article id="n">...</article>
</articles>

?
According to the xml specification an xml document has exactly one document element, not more.

<?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;
}

prints

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

You are getting a nested element, whereas you just need a new one in the same level of the first. So just use

$dom->appendChild($article);

instead of

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

I tried it with your script and I get:

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

So the full code would be:

<?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");

Info taken from DOMDocument::createElement in php.net in the first full example: Example #1 Creating a new element and inserting it as 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();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM