simplexml


how can i add the namespace name before the attribute in xml using simplexml


how can i add namespace name before a attribute of newly created tag,like
<Data ss:Type="String">this the value of data</Data>
but can create only
<Data Type="String">this the value of data</Data>
so i can not add "ss:" before atttbute.
Thanks in Advance
Have Dream Day
In the SimpleXml PHP manual someone gave a good example in the comments section.
Here is how I got it to work.
Say you have the following XML in a file xml.php:
<?php
$string = <<<XML
<Row>
<Cell>
<Data>Dolly Parton</Data>
</Cell>
<Cell>
<Data>Islands in the Stream</Data>
</Cell>
<Cell>
<Data>what</Data>
</Cell>
<Cell>
<Data>1-29-2010</Data>
</Cell>
</Row>
XML;
You can read in this file and add the attributes you want by using XPath.
<?php
include("xml.php");
$sxe = new SimpleXMLElement($string);
$data = $sxe->xpath('//Data');
foreach ($data as $value)
{
$value->addAttribute('ss:Type', 'String', 'http://www.w3.org/2001/XMLSchema-instance');
}
echo $sxe->asXML();
?>
This is the output I get:
<Row>
<Cell>
<Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">Dolly Parton</Data>
</Cell>
<Cell>
<Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">Islands in the Stream</Data>
</Cell>
<Cell>
<Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">what</Data>
</Cell>
<Cell>
<Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">1-29-2010</Data>
</Cell>
</Row>

Related Links

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

Categories

HOME
maven
twitter
kde
json.net
include
ckeditor
yum
flyway4
paradox
esper
spring-kafka
kentor-authservices
wheelnav.js
php-7.1
apache-cayenne
timeout
zebra-printers
http-status-code-504
jprofiler
introduction
jtextfield
dcevm
mustache.php
blazemeter
angular2-aot
smb
quote
srcset
r-raster
google-rich-snippets
twilio-api
jspresso
jmonkeyengine
dosbox
service-discovery
ssjs
dism
preconditions
fedex
no-www
flashair
businessworks
sencha-touch-2.3
qsslsocket
prolog-setof
pebble-watch
rdfs
snmptrapd
google-perftools
nsarray
qtwebview
pearson
sqldf
nbconvert
quartz-composer
word-vba-mac
libpng
infix-notation
url-masking
pcf
storekit
whois
dlna
bluemix-app-scan
goose
associative-array
pyke
spim
ora-00900
mutation-observers
ionic
service-accounts
ami
tt-news
angularjs-ng-click
winddk
system.net.webexception
seaside
oracle-warehouse-builder
srs
odata4j
buster.js
hungarian-algorithm
limejs
tinn-r
trusted
code-cleanup
nsdatecomponents
stage
locate
iphone-web-app
lpeg
coda-slider
mozilla-prism
thunderbird-lightning
firefox4
avatar
ubuntu-9.04
ugc

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