如果节点存在简单XML,如何删除该节点 [英] How to remove a node if it exists with simpleXML

查看:54
本文介绍了如果节点存在简单XML,如何删除该节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样我就知道了我要尝试删除的节点的确切路径。我正在迭代几个XML文件以更新一些内容。为了在中添加一些新内容,我必须首先删除已存在的内容。

以下是我尝试使用的代码,但收到分析错误:语法错误,意外的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]);

            }

        }

    }

推荐答案

unset是语言构造,不能在if语句中使用。如果您在if语句之外使用它/不期望它返回任何东西,则应该没有问题。

这篇关于如果节点存在简单XML,如何删除该节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆