0

私のxml構造は次のとおりです。

<users>
    <user id="126">
        <name>老黄牛三</name>
        <watchHistory>
             <whMonthRecords month="2010-10">
                    <whDateList month="2010-10">
                        <date>01</date>
                        <date>02</date>
                        <date>05</date>
                        <date>08</date>
                        <date>21</date>
                    </whDateList>
                    <whDateRecords date="2010-10-01">
                        <item itemID="1">飞越疯人院.老黄牛三.2010-10-01</item>
                        <item itemID="4">回到未.老黄牛三.2010-10-01来&lt;/item>
                        <item itemID="5">天天看的哦啊你.2010-10-01来&lt;/item>
                    </whDateRecords>
                    <whDateRecords date="2010-10-05">
                        <item itemID="1">飞越疯人院.老黄牛三.2010-10-05</item>
                        <item itemID="4">回到未来.老黄牛三.2010-10-05</item>
                    </whDateRecords>
                </whMonthRecords>
            <whMonthRecords month="2010-11">
             ........
            </whMonthRecords>
       <watchHistory>
    </user>
</users>

さて、どうすれば子を追加できますか:

<whDateRecords date="2010-10-06">
                    <item itemID="45">飞越疯人院.老黄牛三.2010-10-05</item>
                    <item itemID="432">回到未来.老黄牛三.2010-10-05</item>
                </whDateRecords>

ノードへ:<whMonthRecords month="2010-10">

どうもありがとうございます!

4

1 に答える 1

1

まず、追加するノードの親を探します。たとえば、2010-10か月のノードに追加する場合は、次のxpathを使用します。

$xpath = '//whMonthRecords[@month="2010-10"]';
$nodes = $sxml->xpath($xpath); //sxml is the xml object!
$parent = $nodes[0];

親ができたので、addChildメソッドを使用してノードを追加できます。

于 2010-11-21T07:12:16.380 に答える