php - Php remove node from xml containing string
问题描述
I have an XMl file with url nodes. I need to find url node containing the word "Server" and remove them from the file.
<?php
$document = new DOMDocument();
$document->load("../xml/cars689737484897767756.xml");
$xpath = new DOMXpath($document);
foreach ($xpath->query('.//images[contains(., "Server")]') as $dataNode) {
$dataNode->parentNode->removeChild($dataNode);
}
$document->saveXML();
?>
It does not save the removed node.
解决方案
$document->saveXML();
返回结果字符串。您需要以您需要的方式处理该结果。一种方法可能是file_put_contents()
file_put_contents('/path/to/file.xml', $document->saveXML());
或者你也可以DOMDocument::save()
像这样使用:
$document->save('/path/to/file.xml');
但前者提供了更多控制权,因为可以选择要转储的节点。
推荐阅读
- c++ - 自定义预期失败的完整错误消息 (boost::spirit::x3)
- react-native - 如何根据内容使 react-native-elements Tooltip 大小动态化?
- node.js - 如何在我的反应应用程序中将缓冲区数据显示为 Pdf 文档?
- python - 我收到“SyntaxError:无效语法”我没有发现任何错误
- flutter - 当手机有缺口时如何使用 CustomAppBar?
- ios - Xcode 错误:执行“flutter run”命令时
- json - 如何使用 dart 打印 json 数据中的部分值
- python - 如何删除或过滤列表中的非英文(中文、韩文、日文、阿拉伯文)字符串?
- java - 如何在没有数据提取器的情况下为 graphql-java 配置 REST 控制器?
- c - 如何在接收 SIGSTOP 时获取发件人 pid?