0

私は XML と PHP で練習しています。xml ノード全体を削除する方法がわかりません。ここに私のXMLがあります:

<?xml version="1.0" encoding="UTF-8"?>
<items>
<playlist_name>Channel List</playlist_name> 
<category>
<category_id>1</category_id>
<category_title>HD</category_title>
</category>
<channel>
<title>VTC HD1</title>
<stream_url><![CDATA[http://203.162.16.22:80/lives/origin01/vtchd1.isml/vtchd1.m3u8]]></stream_url> 
<logo_30x30>vtchd1.jpg</logo_30x30>
<category_id>1</category_id>
</channel>
<channel>
<title>VTC HD2</title>
<stream_url><![CDATA[http://203.162.16.22:80/lives/origin01/vtchd2.isml/vtchd2.m3u8]]></stream_url> 
<logo_30x30>vtchd2.jpg</logo_30x30>
<category_id>1</category_id>
</channel>
</items>

そして、ここに私が問題を抱えている2つの関数delete()とedit()のコードがあります

<?php

function delete_channel()
{
    $file = "nStream.xml";
    $fp = fopen($file, "rb") or die("cannot open file");
    $str = fread($fp, filesize($file));

    $xml = new DOMDocument();
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->loadXML($str) or die("Error");

    // original
//      echo "<xmp>OLD:\n". $xml->saveXML() ."</xmp>";

    // get document element
    $root   = $xml->documentElement;
    $fnode  = $root->firstChild;

    //get a node
    $ori    = $fnode->childNodes->item(0);

    // remove
    $fnode->removeChild($ori);

//      echo "<xmp>NEW:\n". $xml->saveXML() ."</xmp>";
}

function edit_channel()
{
    echo 'Go Edit'; 
}   

delete_channel() の場合、その関数を実行しても何も削除されません。その関数を使用するたびに、xml ファイル内の 1 つが削除されます。

4

1 に答える 1