如何使用 simpleXML 删除节点(如果存在)
所以我知道我将尝试删除的节点的确切路径。我正在迭代几个 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]);
}
}
}
So I know the exact path the node I would be attempting to remove. I am iterating through several xml files to update some of the content. In order to add some of the new content in, I must first delete the content that already exists.
Here is the code I attempted to use, but I'm receiving a Parse error: syntax error, unexpected 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]);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
unset
是一种语言构造,您不能在if
语句中使用它。如果您在if
语句之外使用它/不要期望它返回任何内容,您应该没问题。unset
is a language construct, and you cannot use it in anif
statement. If you use it outside anif
statement / don't expect it to return anything you should be fine though.