PHP domDocument 删除子节点的子节点
如何删除子节点的父节点,但保留所有子节点?
XML 文件是这样的:
<?xml version='1.0'?>
<products>
<product>
<ItemId>531<ItemId>
<modelNumber>00000</modelNumber>
<categoryPath>
<category><name>Category A</name></category>
<category><name>Category B</name></category>
<category><name>Category C</name></category>
<category><name>Category D</name></category>
<category><name>Category E</name></category>
</categoryPath>
</product>
</products>
基本上,我需要删除categoryPath 节点和category 节点,但将所有名称节点保留在product 节点内。我的目标是这样的文档:
<?xml version='1.0'?>
<products>
<product>
<ItemId>531<ItemId>
<modelNumber>00000</modelNumber>
<name>Category A</name>
<name>Category B</name>
<name>Category C</name>
<name>Category D</name>
<name>Category E</name>
</product>
</products>
Is there PHPbuilt in function to do this?任何指针将不胜感激,我只是不知道从哪里开始,因为有很多子节点。
谢谢
How do I remove a parent node of a child node, but keep all the children?
The XML file is this:
<?xml version='1.0'?>
<products>
<product>
<ItemId>531<ItemId>
<modelNumber>00000</modelNumber>
<categoryPath>
<category><name>Category A</name></category>
<category><name>Category B</name></category>
<category><name>Category C</name></category>
<category><name>Category D</name></category>
<category><name>Category E</name></category>
</categoryPath>
</product>
</products>
Basically, I need to remove the categoryPath node and the category node, but keep all of the name nodes inside of the product node. What I am aiming for is a document like this:
<?xml version='1.0'?>
<products>
<product>
<ItemId>531<ItemId>
<modelNumber>00000</modelNumber>
<name>Category A</name>
<name>Category B</name>
<name>Category C</name>
<name>Category D</name>
<name>Category E</name>
</product>
</products>
Is there PHP built in function to do this? Any pointers would be appreciated, I just do not know where to start because there are many child nodes.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处理 XML 数据的一个好方法是使用 DOM 工具。
一旦你了解了它,就很容易了。例如:
查看实际操作。
A good approach to process XML data is to use the DOM facility.
It's quite easy once you get introduced to it. For example:
See it in action.