使用此帖子xml split node on .中提供的代码,如下所示,当我通过添加说明"颜色"来扩展字符串时它将结果放在父项节点
之外 <?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字符串添加颜色
<?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>
正如你所看到的那样,它被放置了#34;颜色&#34;在</item>
之后它应该在</item>
产品xml文件有650个条目,因此我不确定这是否正确
希望这是足够的信息 - 谢谢
答案 0 :(得分:1)
方法SimpleXMLElement::addChild
适用于父元素。
E.g。在你的(不工作)例子中:
$new_xml->addChild ('colour', $data['item']['colour']);
父元素在$new_xml
范围内。如果您不想将<color>
子项添加到该父项,请选择其他元素作为父项。最佳:选择正确的父元素。
访问SimleXMLElement是part of the basic usage examples。
这里有一个关于如何使用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