2

Teaching myself PHP this week, and as a test project, I've been building a very simple microblog that uses XML data to store/retrieve short post information. I referenced this question and it managed to get me to the point of producing an XML document that resembled what I wanted.

However, I ran into one issue I couldn't figure out on my own. In the linked solution, the same object is updated over and over, without any new information being put into it:

Ex, the 'third test post':

<postslist>
    <post>
        <name>Third Post</name>
        <date>2013-11-05</date>
        <time>00:00</time>
        <text>There is some more post text here.</text>
    </post>
</postslist>

And the 'fourth test post':

<postslist>
    <post>
        <name>Fourth Post</name>
        <date>2013-11-05</date>
        <time>00:00</time>
        <text>There is even more post text here.</text>
    </post>
</postslist>

My PHP, thusfar, resembles this:

        $postname = $_POST["name"];
        $postdate = $_POST["date"];
        $posttime = $_POST["time"];
        $posttext = $_POST["posttext"];

        $postname = htmlentities($postname, ENT_COMPAT, 'UTF-8', false);
        $postdate = htmlentities($postdate, ENT_COMPAT, 'UTF-8', false);
        $posttime = htmlentities($posttime, ENT_COMPAT, 'UTF-8', false);
        $posttext = htmlentities($posttext, ENT_COMPAT, 'UTF-8', false);

        $xml = simplexml_load_file("posts.xml");

        $xml->post = "";
        $xml->post->addChild('name', $postname);
        $xml->post->addChild('date', $postdate);
        $xml->post->addChild('time', $posttime);
        $xml->post->addChild('text', $posttext);

        $doc = new DOMDocument('1.0');
        $doc->formatOutput = true;
        $doc->preserveWhiteSpace = true;
        $doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
        $doc->save('posts.xml');

What I'm hoping to do is create multiple "post" elements, and add the children only to the newest element.

Any help/tips would be appreciated.

4

2 回答 2

1

首先,你不应该混合simplexml_DOMDocument函数。前者是后者的包装(而且,在我看来,不是一个特别好的包装)。如果我是你,我只会使用DOMDocument.

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;

$doc->load('posts.xml', LIBXML_NOBLANKS); // load the posts file with DOMDocument

$newPost = $doc->createElement('post'); // create a new <post> element
$newPost->appendChild($doc->createElement('name', $postname));
$newPost->appendChild($doc->createElement('date', $postdate));
$newPost->appendChild($doc->createElement('time', $posttime));
$newPost->appendChild($doc->createElement('text', $posttext));

$document->documentElement->appendChild($newPost); // add the new <post> to the document

$doc->save('posts.xml');
于 2013-11-01T13:34:48.357 回答
0

您需要先打开文件以便编辑它,否则您将一直替换整个文档而不是添加到它。

这是一个关于它如何与 SimpleXML 一起使用的简短示例,到目前为止仍然足够简单,可以完成这项工作:

$file = 'posts.xml';

$xml  = simplexml_load_file($file); // load existing file
$post = $xml->addChild('post'); // add new post child

// assign values to the post object:
$post->name = $_POST["name"];
$post->date = $_POST["date"];
$post->time = $_POST["time"];
$post->text = $_POST["posttext"];

$xml->saveXML($file); //save file with changes

...并且完全兼容它的姊妹库 DOMDocument,以防您需要那里的一些功能。它们共享相同的内存对象。

于 2013-11-02T14:07:44.333 回答