gpt4 book ai didi

每个父节点的 PHP replaceChild

转载 作者:可可西里 更新时间:2023-10-31 23:09:13 30 4
gpt4 key购买 nike

我正在使用此 PHP 代码替换 XML 文件中的一系列节点:

<?php
$xml = "
<products>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
</products>
";
function renameTags($xml, $old, $new)
{
$dom = new DOMDocument();
$dom->loadXML($xml);

$nodes = $dom->getElementsByTagName($old);

$toRemove = array();

foreach ($nodes as $node)
{
$newNode = $dom->createElement($new);

foreach ($node->attributes as $attribute)
{
$newNode->setAttribute($attribute->name, $attribute->value);
}

foreach ($node->childNodes as $child)
{
$newNode->appendChild($node->removeChild($child));
}

$node->parentNode->appendChild($newNode);
$toRemove[] = $node;
break;

}

foreach ($toRemove as $node)
{
$node->parentNode->removeChild($node);

}

$dom->formatOutput = TRUE;
return $dom->saveXML();
}

$xml = renameTags($xml, 'name', 'Source');
$xml = renameTags($xml, 'name', 'parentCategory');
$xml = renameTags($xml, 'name', 'Category');
$xml = renameTags($xml, 'name', 'subCategory');
$xml = renameTags($xml, 'name', 'sisterCategory');

echo $xml;
?>

但是,此代码仅替换且仅适用于一个产品集。我想我需要在由 foreach parentNode('product') 替换标签名称组成的函数中的某处调用 foreach 语句。我需要为迭代所有产品集的函数添加什么?

上面的代码将打印:(注意第一组名称节点已经不存在了,取而代之的是指定的新节点)

<?xml version="1.0"?>
<products>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<Source>Buy</Source>
<parentCategory>Car, Marine &amp; GPS</parentCategory>
<Category>Car Installation Parts</Category>
<subCategory>Deck Installation Parts</subCategory>
<sisterCategory>Antennas &amp; Adapters</sisterCategory></product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
</products>

如您所见,它只替换了一个产品实例。

最佳答案

foreach 中的中断是您的问题:它在第一个 <name> 处停止它遇到的节点,不管它在文档中的什么位置。

首先您必须遍历产品,然后遍历产品的 <name>节点。类似于以下内容:

function renameTags($xml, $old, $new) {
$toRemove = array();
$dom = new DOMDocument();
$dom->loadXML($xml);
$products = $dom->getElementsByTagName('product');

foreach($products as $product) {
$nodes = $product->getElementsByTagName($old);

foreach ($nodes as $node) {
$newNode = $dom->createElement($new);

foreach ($node->attributes as $attribute) {
$newNode->setAttribute($attribute->name, $attribute->value);
}

foreach ($node->childNodes as $child) {
$newNode->appendChild($node->removeChild($child));
}

$node->parentNode->appendChild($newNode);
$toRemove[] = $node;

break;
}
}

foreach ($toRemove as $node) {
$node->parentNode->removeChild($node);
}

$dom->formatOutput = TRUE;
return $dom->saveXML();
}

关于每个父节点的 PHP replaceChild,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8416151/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com