父项外的 XML 节点 [英] XML Node outside parent item
问题描述
使用这篇文章中提供的代码xml split node on .如下我通过添加颜色"来扩展字符串,它将结果放在父项节点之外
Using the code provided in this post xml split node on . as below when I go to extend the string by add say "colour" it places the result outside the parent item node
<?php
$xml_string = '<product><item><partno>abc123</partno><Compatbility>model1: 110C, 115C, 117C. model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</Compatbility></item></product>';
$original_xml = simplexml_load_string($xml_string);
$data = json_decode(json_encode($original_xml), true);
$compatbility = $data['item']['Compatbility']; // get all compatibility values
// explode values
$compatbility = array_filter(array_map('trim', explode('.', $compatbility)));
$new_xml = new SimpleXMLElement('<product/>'); // initialize new xml
// add necessary values
$new_xml->addChild('item')->addChild('partno', $data['item']['partno']);
$new_xml->item->addChild('Compatbility');
// loop the values and add them as children
foreach($compatbility as $value) {
$value = trim(preg_replace('/(\w+):/', '', $value));
$new_xml->item->Compatbility->addChild('model', $value);
}
echo $new_xml->asXML(); // output as xml
?>
修改后的代码,为 xml 字符串添加颜色
Revised code adding colour to the xml string
<?php
$xml_string = '<product><item><partno>abc123</partno><colour>black</colour><Compatbility>model1: 110C, 115C, 117C. model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</Compatbility></item></product>';
$original_xml = simplexml_load_string($xml_string);
$data = json_decode(json_encode($original_xml), true);
$compatbility = $data['item']['Compatbility']; // get all compatibility values
// explode values
$compatbility = array_filter(array_map('trim', explode('.', $compatbility)));
$new_xml = new SimpleXMLElement('<product/>'); // initialize new xml
// add necessary values
$new_xml->addChild('item')->addChild('partno', $data['item']['partno']);
$new_xml->addChild ('colour', $data['item']['colour']);
$new_xml->item->addChild('Compatbility');
// loop the values and add them as children
foreach($compatbility as $value) {
$value = trim(preg_replace('/(\w+):/', '', $value));
$new_xml->item->Compatbility->addChild('model', $value);
}
echo $new_xml->asXML(); // output as xml
?>
和 XML 输出
<product>
<item>
<partno>abc123</partno>
<Compatbility><model>110C, 115C, 117C</model>
<model>1835C, 1840C</model>
<model>210C, 215C, 3240C</model>
</Compatbility>
</item>
<colour>black</colour>
</product>
如您所见,它在 </item>
之后放置了颜色",而它应该位于 </item>
As you can see it's placed the "colour" after the </item>
when it should be inside the </item>
产品 xml 文件有 650 个条目,所以我不确定这是否正确
The product xml file has 650 entries so i'm not sure this is right anyway
希望这是足够的信息 - 谢谢
Hope this is enough info - thanks
推荐答案
SimpleXMLElement::addChild
作用于父元素.
The method SimpleXMLElement::addChild
works on the parent element.
例如在您的(不工作)示例中:
E.g. in your (not working) example:
$new_xml->addChild ('colour', $data['item']['colour']);
父元素在 $new_xml
中.如果您不想将
子元素添加到该父元素,请选择其他元素作为父元素.最佳:选择正确的父元素.
The parent element is within $new_xml
. If you don't want to add the <color>
child to that parent, choose a different element as parent. Best: Choose the right parent element.
访问 SimleXMLElement 是基本用法示例的一部分.
Accessing a SimleXMLElement is part of the basic usage examples.
这里有一个关于如何使用 simplexml 将子元素添加到特定父元素的示例:
So here an example on how to add a children to a specific parent element with simplexml:
<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<title>PHP: Behind the Parser</title>
<characters>
<character>
<name>Ms. Coder</name>
<actor>Onlivia Actora</actor>
</character>
<character>
<name>Mr. Coder</name>
<actor>El ActÓr</actor>
</character>
</characters>
<plot>
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
</plot>
<great-lines>
<line>PHP solves all my web problems</line>
</great-lines>
<rating type="thumbs">7</rating>
<rating type="stars">5</rating>
</movie>
</movies>
XML;
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->plot; # So, this language. It's like, a programming language. Or is it a ...
$movie = $movies->movie[0];
$movie->addChild('color', 'technicolor'); # added color child to the move element
这篇关于父项外的 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!