simplexml


Simplexml Insert into sub element


i have an xml file same bellow:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<prod id="1">
<layer id="layer_0" imgSRC="data/361_layer_0.png"/>
<layer id="layer_1" imgSRC="data/362_layer_0.png"/>
<layer id="layer_2" imgSRC="data/363_layer_0.png"/>
</prod>
<prod id="2">
<layer id="layer_0" imgSRC="data/361_layer_0.png"/>
<layer id="layer_1" imgSRC="data/362_layer_0.png"/>
<layer id="layer_2" imgSRC="data/363_layer_0.png"/>
</prod>
</data>
how to use simplexml to insert an item into layer id="layer_3" like bellow example:
<prod id="1">
<layer id="layer_0" imgSRC="data/361_layer_0.png"/>
<layer id="layer_1" imgSRC="data/362_layer_0.png"/>
<layer id="layer_2" imgSRC="data/363_layer_0.png"/>
<layer id="layer_3" imgSRC="data/364_layer_0.png"/>
</prod>
what do i have to do?
Thanks!!
(1) select the node that will be the parent of the new child, I'd go with xpath:
$xml = simplexml_load_string($x); // assuming XML in $x
$node = $xml->xpath("//prod[#id='2']/layer[#id='layer_2']")[0];
xpath will select all <layer> nodes with id=layer_2 that have <prod> with id=2 as a parent. In case there are several results, the [0] will select just the first one. This works with PHP >= 5.4.
see this working: http://3v4l.org/5Q46F
(2) then add the child and its attributes, see example #10 in the simplexml manual:
http://www.php.net/manual/en/simplexml.examples-basic.php

Related Links

SimpleXML extract text between two tags
XML Node outside parent item
SimpleXMLElement cannot get the output
SimpleXML changing parsed decimals
can't navigate SimpleXMLElement data
PHP SimpleXML Load File with Null elements
Iterating through and editing a SimpleXML object by reference in PHP
Simplexml Insert into sub element
warning: simplexml_load_file(): I/O warning : failed to load external entity drupal [closed]
What's wrong with this code? SimpleXMLElement [closed]
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

Categories

HOME
wso2
ngrx
heroku
push-notification
react-virtualized
vmware
programming-languages
webpack-2
ckeditor
blueprintjs
convolution
uber-api
gitpitch
ssl-client-authentication
row
quickbooks
visual-studio-2005
ibm-odm
visual-studio-cordova
django-simple-history
oxyplot
emulator
cx-freeze
sox
php-openssl
p-value
tooltipster
minitab
libssl
skia
siesta-swift
catel
webtest
windows-dev-center
uiswipegesturerecognizer
geopositioning
overlap
swisscomdev
hilbert-curve
unobtrusive-validation
opshub
mapzen
eclipse-scout
ncalc
nomethoderror
mime
flashair
google-datalayer
pubmed
qsslsocket
fancybox-2
idisposable
pg-dump
r-forge
qtwebview
nbconvert
dynamics-sl
objective-c-swift-bridge
messenger
jquery-filter
deadbolt-2
azure-virtual-network
player
fuzzy-search
packagist
system.management
uid
historian
tcpserver
cannon.js
qdialog
sframe
mmc
dundas
angular-local-storage
web-controls
fscommand
castle
google-reader
typo3-neos
viadeo
farseer
quartz-graphics
shellexecute
hungarian-algorithm
libstdc++
armcc
newtonscript
onsubmit
parametric-equations
enter
vdsp
jmock
site.master
castle-monorail
substrings
ctp4
xetex
private-members

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