XML Node outside parent item

2019-09-01 02:49发布

问题:

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
?>

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
?>

And the XML output

    <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>

As you can see it's placed the "colour" after the </item> when it should be inside the </item>

The product xml file has 650 entries so i'm not sure this is right anyway

Hope this is enough info - thanks

回答1:

The method SimpleXMLElement::addChild works on the parent element.

E.g. in your (not working) example:

$new_xml->addChild ('colour', $data['item']['colour']);

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.

Accessing a SimleXMLElement is part of the basic usage examples.

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&#211;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