PHP DOM +在xml中删除特定节点
我想根据我在 xml 中传递的 id 删除 XML 中的特定节点。 以下似乎不起作用,请帮助我。
$id = $_GET['nodeId'];
$dom = new DOMDocument;
$dom->load('Seat_matrix.xml');
$xpath = new DOMXPath($dom);
$query = sprintf('/location/cubicle[./id = "%d"]', $id);
foreach($xpath->query($query) as $cubicle) {
$record->parentNode->removeChild($cubicle);
}
$dom->save("Seat_matrix.xml");
XML文件
<location>
<cubicle>
<id> 6121</id>
<status>2</status>
<shift1>
<Employee1>Tom</Employee1>
</shift1>
<shift2>
<Employee2>arum</Employee2>
</shift2>
</cubicle>
</location>
I want to delete particular node in XML based on the id I passed in the xml.
The below seems not working please help me.
$id = $_GET['nodeId'];
$dom = new DOMDocument;
$dom->load('Seat_matrix.xml');
$xpath = new DOMXPath($dom);
$query = sprintf('/location/cubicle[./id = "%d"]', $id);
foreach($xpath->query($query) as $cubicle) {
$record->parentNode->removeChild($cubicle);
}
$dom->save("Seat_matrix.xml");
XML file
<location>
<cubicle>
<id> 6121</id>
<status>2</status>
<shift1>
<Employee1>Tom</Employee1>
</shift1>
<shift2>
<Employee2>arum</Employee2>
</shift2>
</cubicle>
</location>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将 sprintf 与 %d 一起使用,但元素节点包含“ 6121”(注意开头的空格)。
请改用 %s。还。没有
$record
变量,因此您必须将代码更改为You are using sprintf with %d but the element node contains " 6121" (mind the space in the beginning).
Use %s instead. Also. there is no
$record
variable, so you have to change the code to