【问题标题】:How to remove a node if it exists with simpleXML如果使用 simpleXML 存在节点,如何删除它
【发布时间】:2011-03-25 23:53:33
【问题描述】:

所以我知道我将尝试删除的节点的确切路径。我正在遍历几个 xml 文件以更新一些内容。为了添加一些新的内容,我必须先删除已经存在的内容。

这是我尝试使用的代码,但我收到 Parse 错误:语法错误,意外的 T_UNSET

public function hint_insert() {

    foreach($this->hints as $key => $value) {

        $filename = $this->get_qid_filename($key);

        echo "$key - $filename - $value[0]<br>";

        //insert hint within right node using simplexml
        $xml = simplexml_load_file($filename);

        foreach ($xml->PrintQuestion as $PrintQuestion) {

            if (unset($xml->PrintQuestion->content->multichoice->feedback->hint->Passage)) {

                $xml->PrintQuestion->content->multichoice->feedback->hint->addChild('Passage', $value[0]);

            } else {

                $xml->PrintQuestion->content->multichoice->feedback->hint->addChild('Passage', $value[0]);

            }

        }

    }

【问题讨论】:

    标签: php simplexml unset


    【解决方案1】:

    unset 是一种语言结构,您不能在if 语句中使用它。如果您在 if 语句之外使用它/不要期望它返回任何内容,您应该没问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-27
      • 2018-11-11
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多