4

これはかなり簡単に聞こえるかもしれませんが、それでもこの質問をフォーラムに投稿したいと思います。メイン要素の後にデータを追加し、既存の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>

すべての情報を含む別の製品を追加し、新しく追加されたデータを上部に追加して、新しく追加されたデータがヘッダーコンテンツの後に来るようにしたいと考えています。

追加するデータ:

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

更新された 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>

役立つアドバイスやサンプルコードは本当に役に立ちます。

編集:

申し訳ありませんが、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');
    }
?>

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

何かアドバイス?

4

1 に答える 1

10

これは機能します。

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

お役に立てれば。

于 2012-06-14T12:07:00.100 に答える