使用php更新/附加数据到xml文件

125*_*369 4 php xml simplexml

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

要添加的数据:

        <product num="2103">
            <name>AGB</name>
            <category>Movies</category>
            <available content="YES"></available>
        </product>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

编辑:

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

谢谢

<?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');
    }
?>
Run Code Online (Sandbox Code Playgroud)

我的xml文件得到了更新,但是数据被添加到第一个子节点,而且底部也是如此,我想在之后和之后添加数据,如上所示.这是我的输出:

<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>
Run Code Online (Sandbox Code Playgroud)

有什么建议?

web*_*mad 8

这会奏效.

<?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');
}
?>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.