0

我想从我的 XML 文件中删除一个特定的节点,似乎我错过了代码中的某些内容!你能帮我弄清楚吗,例如我想删除标签及其所有内部标签。

XML结构:

    <students>
    <student>
     <username>sally</username>
     <subjects>
    <subjects>sub1</subjects>
    <subjects>sub2</subjects>
    <subjects>sub3</subjects>
     </subjects>
    </student>
<student>
     <username>jojo</username>
     <subjects>
    <subjects>sub1</subjects>
    <subjects>sub2</subjects>
    <subjects>sub3</subjects>
 <subjects>sub4</subjects>
 <subjects>sub5</subjects>
     </subjects>
    </student>
    </students>

功能 :

 public static function delete_subjects($UserName, $fileName) {
        //delete from xml
        $xml = new DOMDocument();
        $xml->load($fileName);

        $users = $xml->getElementsByTagName('student');
        foreach ($users as $user) {
          $username = $user->getElementsByTagName('username')->item(0)->nodeValue;
          if ($username == $UserName) {
                $p=$user->getElementsByTagName('subjects')->item(0);
                $xml->documentElement->removeNode($p);

                break;
            }
        }
        $xml->save($fileName);
    }

称呼 :

delete_subjects('jojo', $fileName);
4

1 回答 1

0

而不是$xml->documentElement->removeNode($p);尝试使用$user->removeChild($p);

编辑:甚至$p->parentNode->removeChild($p);

于 2012-12-08T14:05:50.823 回答