simplexml


PHP SimpleXML Load File with Null elements


I have the following XML file called 'cookie_domain.xml' with the contents:
<?xml version="1.0" encoding="UTF-8"?>
<setting>
<parameter>cookie_domain</parameter>
<displayname>Cookie Domain</displayname>
<grouping>Sessions</grouping>
<selecttype>text</selecttype>
<setting />
<help>Domain that the cookie is valid for</help>
</setting>
which I load into an object using:
$xml_object = simplexml_load_file('cookie_domain.xml');
The problem is that I want the 'setting' element to be null as specified in the XML, but what I get from the object, when I turn it into an array, is:
Array
(
[parameter] => cookie_domain
[displayname] => Cookie Domain
[grouping] => Sessions
[selecttype] => text
[setting] => SimpleXMLElement Object
(
)
[help] => Domain that the cookie is valid for
)
Is there anyway to get SimpleXML to honour the 'null' value instead of putting a 'SimpleXMLElement Object' in there? So I would end up with:
Array
(
[parameter] => cookie_domain
[displayname] => Cookie Domain
[grouping] => Sessions
[selecttype] => text
[setting] =>
[help] => Domain that the cookie is valid for
)
I am using this information to import into a database and the Object is causing issues as I need the element to be there, even if it is 'null' as this is valid in my application.
Thanks very much,
Russell
In XML, a self-closing element (e.g. <foo />) is equivalent to one with empty contents (e.g. <foo></foo>) so if you want this to translate as a php NULL value, you will have to check for the contents being an empty string.
A simple way of getting what you want given the example you've posted would be to loop over every element in the document setting an array key to the appropriate string, and replacing empty strings with NULL:
$settings_array = array();
foreach ( $xml_object->children() as $tag_name => $element )
{
$settings_array[$tag_name] = trim( (string)$element );
if ( strlen($settings_array[$tag_name]) == 0 )
{
$settings_array[$tag_name] = NULL;
}
}
Here is a live demo.

Related Links

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
simplexml_load_file failed to open stream Http request failed

Categories

HOME
log4j
netbeans
deezer
heroku
onedrive
cplex
cmd
alpha
autotools
cross-validation
row
windows-azure-storage
ef-migrations
core-text
numerical-methods
dynamic-featured-image
wtx
.net-4.0
copying
perlin-noise
webtest
stringtemplate
subset-sum
x11-forwarding
objectlistview
bootstrapper
tasker
ibpy
osx-mavericks
rails-routing
blogengine.net
iso8601
spring-cache
chord-diagram
smart-table
root-framework
watchconnectivity
vmware-tools
slicknav
home-directory
adxstudio-portals
pcf
xml-attribute
fputcsv
python-stackless
r-tree
content-length
jqgrid-formatter
coveralls
embedded-code
deis
arcanist
operation
system32
device-orientation
umbraco6
facebook-graph-api-v2.4
winddk
javafx-webengine
cctv
wordpress-theme-customize
terminfo
xojo
mesa
transcoding
viadeo
mysqltuner
oam
google-cloud-save
blending
buster.js
padarn
yui-compressor
xamlparseexception
libstdc++
hosts-file
bubble-chart
nsmanagedobject
work-stealing
nsdatecomponents
ecl
krl
cxxtest
sudzc
dbal
ext3
perfect-hash

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