simplexml


XML Node outside parent item


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

Related Links

Get children attributes using simplexml
Select attribute in simplexml
Save formatted xml
SimpleXML (Zend_Config_Xml actually) and foreach : which tag am I iterating?
simplexml_load_file() error happening in only localhost/xampp
SimpleXml tar.gz
simplexml_load_file error in PHP 5.3
simplexml_load_file not a valid URI issue
Can't turn XML response into an simplexml object
Having trouble getting Fogbugz API response into a SimpleXML object
Upgraded to php 5.3.10 and simpleXMLelement is broken
Parsing RSS Feed with SimpleXML
php simplexml_load_file (script url) failed to open stream
PHP - SimpleXML not returning object
Having trouble processing XML with PHP SimpleXML
Simplexml How to access all element of the same node

Categories

HOME
pypi
kde
mfc
comparison
session
electron
q
rdf
dxl
alpha
yum
correlation
google-apps-marketplace
n-gram
systemc
reactcsstransitiongroup
introduction
accessor
kudan
restful-authentication
facet
django-storage
chromebook
maquette
ping
windowbuilder
javascriptcore
pdb
noraui
uiswipegesturerecognizer
android-kernel
overlap
io-redirection
swisscomdev
serverside-rendering
twitch
unobtrusive-validation
squib
nand2tetris
keydown
outlook-api
slick-3.0
segment
jquery-validate
sage-one
winscp-net
idisposable
gabor-filter
crypt
flutterwave
nbconvert
mcafee
lync-client-sdk
google-cdn
log4c
trash
angstrom-linux
livequery
javax.sound.midi
google-places
spidermonkey
content-length
emailrelay
responsive-images
microbenchmark
ideamart
myo
method-parameters
system32
fscommand
preferences
zend-route
dml
picturefill
oracle-warehouse-builder
intentservice
imdbpy
ccss
quartz-graphics
eclipse-memory-analyzer
sharp-repository
runas
icanhaz.js
perfect-hash
data-driven
spec#

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App