php删除子节点但保留其子节点

php删除子节点但保留其子节点,php,xml,Php,Xml,我使用以下代码: 我试图删除categoryPath节点,但保留其子节点(所有名称标记) 它当前离开了categoryPath,但我正在寻找关于如何删除categoryPath节点但保留其子节点的建议 <?php // load up your XML $xml = new DOMDocument; $xml->load('book.xml'); // Find all elements you want to replace. Since your data is really

我使用以下代码: 我试图删除categoryPath节点,但保留其子节点(所有名称标记) 它当前离开了categoryPath,但我正在寻找关于如何删除categoryPath节点但保留其子节点的建议

<?php

// load up your XML
$xml = new DOMDocument;
$xml->load('book.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');
$categoryPath = $xml->getElementsByTagName('categoryPath');
// 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); 
但它不会删除categoryPath节点

xml:

<?xml version="1.0"?>
<products>
<product>
<bestBuyItemId>531670</bestBuyItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<categoryPath>
<name>ddd</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</categoryPath>
</product>

</products>

531670
METRA电子/移动音频
ddd
汽车,海事及;全球定位系统
汽车安装零件
甲板安装部件
天线及;适配器

您在以下行中遇到问题:

$elements = $xml->getElementsByTagName('category');
什么节点应该是类别?我在xml文件中没有看到任何类别节点

然后,这个代码:

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); 
}
无法确定工作,因为
$elements
为空

您需要选择所有
name
节点,然后将它们附加到
product
节点。比如:

foreach ( $xml->getElementsByTagName('product') as $product ) {
    foreach( $product->getElementsByTagName('name') as $name ) {
        $product->appendChild( $name );
    }
    $product.removeChild( $xml->getElementsByTagName('categoryPath')->item(0) );
}

您在以下行中遇到问题:

$elements = $xml->getElementsByTagName('category');
什么节点应该是类别?我在xml文件中没有看到任何类别节点

然后,这个代码:

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); 
}
无法确定工作,因为
$elements
为空

您需要选择所有
name
节点,然后将它们附加到
product
节点。比如:

foreach ( $xml->getElementsByTagName('product') as $product ) {
    foreach( $product->getElementsByTagName('name') as $name ) {
        $product->appendChild( $name );
    }
    $product.removeChild( $xml->getElementsByTagName('categoryPath')->item(0) );
}

我删除了一个节点。你能告诉我怎么做吗?用一个例子更新了答案。Php远离内存,可能有一些语法错误…它工作了。谢谢你。但它只是附加了3个名称标签?这是应该发生的吗?它应该附加所有名称节点,你能用完整的代码更新你的问题吗?我删除了一个节点。你能告诉我怎么做吗?用一个例子更新了答案。Php远离内存,可能有一些语法错误…它工作了。谢谢你。但它只是附加了3个名称标签?这是应该发生的吗?它应该附加所有的名称节点,你能用完整的代码更新你的问题吗?