使用php更新/追加数据到xml文件 [英] update/append data to xml file using php

查看:44
本文介绍了使用php更新/追加数据到xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来很简单,但我仍然想在论坛上发布这个问题.我有一个 xml 文件,需要在主元素之后附加数据并保存 xml 文件而不覆盖现有的 xml 文件,而是将数据附加到现有数据并更新 xml 文件.

this may sound pretty straight forward, but still I want to post this question in the forum. I have a xml file, which needs to be appended with data after the main element and save the xml file without overwriting the existing xml file but to append the data to already existing data and update the xml file.

例如,我的 xml 数据看起来与此类似:

For example my xml data looks something similar to this:

<maincontent>
    <headercontent>
        <product num="2102">
            <name>MSG</name>
            <category>Wellness</category>
            <available content="YES"></available>
        </product>
        <product num="2101">
            <name>YSD</name>
            <category>Music</category>
            <available content="NO"></available>
        </product>
        <product num="2100">
            <name>RCS</name>
            <category>Media</category>
            <available content="YES"></available>
        </product>
    </headercontent>
</maincontent>

我想添加另一个包含所有信息的产品,并将新添加的数据附加到顶部,以便新添加的数据应该在标题内容之后.

I want to add another product with all the info and append the newly added data at the top so that the newly added data should come after the headercontent.

要添加的数据:

        <product num="2103">
            <name>AGB</name>
            <category>Movies</category>
            <available content="YES"></available>
        </product>

更新后的 xml 文件应如下所示:

The updated xml file should be looking like this as shown below:

<maincontent>
    <headercontent>
        <product num="2103">
                <name>AGB</name>
                <category>Movies</category>
                <available content="YES"></available>
    </product>
        <product num="2102">
            <name>MSG</name>
            <category>Wellness</category>
            <available content="YES"></available>
        </product>
        <product num="2101">
            <name>YSD</name>
            <category>Music</category>
            <available content="NO"></available>
        </product>
        <product num="2100">
            <name>RCS</name>
            <category>Media</category>
            <available content="YES"></available>
        </product>
    </headercontent>
</maincontent>

任何有用的建议或一段示例代码都会非常有帮助.

Any useful advice or a piece of example code would be really helpful.

对不起,我没有发布任何 php 代码,我的错.这是我一直在研究的代码:

sorry guys I haven't posted any php code, my fault. Here is the code which I have been working on:

谢谢

<?php

 $xmldoc = new DomDocument();    
    $xmldoc->formatOutput = true;

    $productNum = "2103";
    $name = "AGB";
    $category = "Movies";
    $content = "YES";

    if($xml = file_get_contents('main.xml')){
        $xmldoc->loadXML($xml);

        $root = $xmldoc->firstChild;        

        $newElement = $xmldoc->createElement('product');
        $root->appendChild($newElement);
        $numAttribute = $xmldoc->createAttribute("num");
        $numAttribute->value = $productNum;
        $newElement->appendChild($numAttribute);   

        $nameElement = $xmldoc->createElement('name');
        $root->appendChild($nameElement);
        $nameText = $xmldoc->createTextNode($name);
        $nameElement->appendChild($nameText);

        $categoryElement = $xmldoc->createElement('category');
        $root->appendChild($categoryElement);
        $categoryText = $xmldoc->createTextNode($category);
        $categoryElement->appendChild($categoryText);

        $availableElement = $xmldoc->createElement('available');
        $root->appendChild($availableElement);
        $availableAttribute = $xmldoc->createAttribute("content");
        $availableAttribute->value = $content;
        $availableElement->appendChild($availableAttribute);   


        $xmldoc->save('main.xml');
    }
?>

我的 xml 文件被更新,但数据被添加到 firstchild 和底部,而不是我想在后面和开头添加数据,如上所示.这是我的输出:

My xml file gets updated but the data is added to the firstchild and that too at the bottom, instead I want to add data after and in the beginning as shown above. Here is my output:

<maincontent>
    <headercontent>
        <product num="2102">
            <name>MSG</name>
            <category>Wellness</category>
            <available content="YES"/>
        </product>
        <product num="2101">
            <name>YSD</name>
            <category>Music</category>
            <available content="NO"/>
        </product>
        <product num="2100">
            <name>RCS</name>
            <category>Media</category>
            <available content="YES"/>
        </product>
    </headercontent>
<product num="2103"/><name>AGB</name><category>Movies</category><available content="YES"/></maincontent>

有什么建议吗?

推荐答案

这会起作用.

<?php
$xmldoc = new DomDocument( '1.0' );
$xmldoc->preserveWhiteSpace = false;
$xmldoc->formatOutput = true;

$productNum = "2103";
$name = "AGB";
$category = "Movies";
$content = "YES";

if( $xml = file_get_contents( 'main.xml') ) {
    $xmldoc->loadXML( $xml, LIBXML_NOBLANKS );

    // find the headercontent tag
    $root = $xmldoc->getElementsByTagName('headercontent')->item(0);

    // create the <product> tag
    $product = $xmldoc->createElement('product');
    $numAttribute = $xmldoc->createAttribute("num");
    $numAttribute->value = $productNum;
    $product->appendChild($numAttribute);

    // add the product tag before the first element in the <headercontent> tag
    $root->insertBefore( $product, $root->firstChild );

    // create other elements and add it to the <product> tag.
    $nameElement = $xmldoc->createElement('name');
    $product->appendChild($nameElement);
    $nameText = $xmldoc->createTextNode($name);
    $nameElement->appendChild($nameText);

    $categoryElement = $xmldoc->createElement('category');
    $product->appendChild($categoryElement);
    $categoryText = $xmldoc->createTextNode($category);
    $categoryElement->appendChild($categoryText);

    $availableElement = $xmldoc->createElement('available');
    $product->appendChild($availableElement);
    $availableAttribute = $xmldoc->createAttribute("content");
    $availableAttribute->value = $content;
    $availableElement->appendChild($availableAttribute);

    $xmldoc->save('main.xml');
}
?>

希望这会有所帮助.

这篇关于使用php更新/追加数据到xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆