PHP domDocument 删除子节点的子节点

发布于 2024-12-19 19:00:30 字数 1190 浏览 1 评论 0原文

如何删除子节点的父节点,但保留所有子节点?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2024-12-26 19:00:30

处理 XML 数据的一个好方法是使用 DOM 工具。

一旦你了解了它,就很容易了。例如:

<?php

// load up your XML
$xml = new DOMDocument;
$xml->load('input.xml');

// Find all elements you want to replace. Since your data is really simple,
// you can do this without much ado. Otherwise you could read up on XPath.
// See http://www.php.net/manual/en/class.domxpath.php
$elements = $xml->getElementsByTagName('category');

// WARNING: $elements is a "live" list -- it's going to reflect the structure
// of the document even as we are modifying it! For this reason, it's
// important to write the loop in a way that makes it work correctly in the
// presence of such "live updates".
while($elements->length) {
    $category = $elements->item(0); 
    $name = $category->firstChild; // implied by the structure of your XML 

    // replace the category with just the name 
    $category->parentNode->replaceChild($name, $category); 
} 

// final result:
$result = $xml->saveXML();

查看实际操作

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:

<?php

// load up your XML
$xml = new DOMDocument;
$xml->load('input.xml');

// Find all elements you want to replace. Since your data is really simple,
// you can do this without much ado. Otherwise you could read up on XPath.
// See http://www.php.net/manual/en/class.domxpath.php
$elements = $xml->getElementsByTagName('category');

// WARNING: $elements is a "live" list -- it's going to reflect the structure
// of the document even as we are modifying it! For this reason, it's
// important to write the loop in a way that makes it work correctly in the
// presence of such "live updates".
while($elements->length) {
    $category = $elements->item(0); 
    $name = $category->firstChild; // implied by the structure of your XML 

    // replace the category with just the name 
    $category->parentNode->replaceChild($name, $category); 
} 

// final result:
$result = $xml->saveXML();

See it in action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文