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

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
wso2-am
wso2
openmp
proxy
image-processing
tizen
xmpp
setup-deployment
microservices
acquia
networkx
bs4
amazon-cloudformation
applepay
kentor-authservices
izpack
google-cloud-ml
gz
percona
openrefine
highlight.js
saxon
finite-automata
samoa
django-cms
core-text
status
phpfox
fgetcsv
fifo
msys2
tasklet
xquery-3.0
create-table
google-qpx-express-api
noraui
code-contracts
hybridauth
cloud-code
bower-install
http-digest
appfabric
git-merge
android-browser
bootstrapper
gulp-sourcemaps
dynamic-reports
keydown
strptime
zip4j
errordocument
return-value
mu
yii2-extension
pearson
dwscript
gridpane
httplistener
dstu2-fhir
superstack
wapiti
emailrelay
processmodel
mmc
remobjects
unity5.2.3
skos
jscript.net
rdl
uitouch
oam
quantlib-swig
gridfs
sabredav
e4x
bitsharp
isnullorempty
datawindow
netdna-api
appendto
ecl
disclosure
inotifycollectionchanged
lang
radcombobox
zend-translate
web-application-design
dentrix
gin
modelstate
mirah
meego
virtual-functions
port-number

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App