simplexml


PHP simplexml Entities


What's going one here?
$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<album>
<img src="002.jpg" caption="wássup?" />
</album>
XML;
$xml = simplexml_load_string($string);
// $xmlobj = simplexml_load_file("xml.xml"); // same thing
echo "<pre>";
var_dump($xml);
echo "</pre>";
Error:
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Entity 'aacute' not defined
&aacute is not an XML entity - you're thinking about HTML.
Special characters are usually used "as is" in XML - an html_entity_decode() on the input data (don't forget to specify UTF-8 as the character set) should do the trick:
$string = html_entity_decode($string, ENT_QUOTES, "utf-8");
i had this problem the other day.
any occurrence of & will need to be inside a CDATA tag
<album>
<img src="002.jpg" />
<caption><![CDATA[now you can put whatever characters you need & include html]]></caption>
</album>
to keep the parser from failing.
You may want to look at Matt Robinson's article on an alternative method: Converting named entities to numeric in PHP . It mentions the html_entity_decode method (already pointed out by another answer) and some potential pitfalls:
There are two possible problems with this approach. The first is invalid entities: html_entity_decode() won't touch them, which means you'll still get XML errors. The second is encoding. I suppose it's possible that you don't actually want UTF-8. You should, because it's awesome, but maybe you have a good reason. If you don't tell html_entity_decode() to use UTF-8, it won't convert entities that don't exist in the character set you specify. If you tell it to output in UTF-8 and then use something like iconv() to convert it, then you'll lose any characters that aren't in the output encoding.
Also, if you find the script rather cumbersome, you can also use the one shared on SourceRally.
Another solution is to change
"wássup?"
to
"w&aacute;ssup?"

Related Links

Copying CDATA between 2 xml files
PHP 7 simpleXML fatal error
SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found
Add xml attribute documentname
Pick shortest route with simpleXML
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

Categories

HOME
pandas
google-play
include
fme
jsp-tags
google-docs
cvs
spring-cloud-stream
sharepoint-designer
rubygems
sqlite3
ebean
ezpublish
directx
amazon-cloudformation
gitpitch
imacros
usergrid
static-libraries
messages
hex-editors
designer
foselasticabundle
saxon
flux
google-pagespeed
emulator
google-search-api
cultureinfo
fish
intel-pin
nhapi
rst2pdf
strncpy
elasticsearch-net
dbclient
gsmcomm
media-queries
data-manipulation
openoffice.org
google-api-nodejs-client
mpmediaquery
x11-forwarding
android-browser
starteam
angularjs-factory
sfdc
reportingservices-2005
spring-security-kerberos
s
menuitem
xml-documentation
apache-fop
rails-routing
colorama
dtexec
python-cryptography
zendesk-app
cudafy.net
or-tools
specrun
objective-c-swift-bridge
nxt
heidisql
bonobo
simplewebrtc
essence
pickadate
livequery
wapiti
pyke
uid
ford-fulkerson
xna-4.0
iis-arr
pundit
wyam
ideamart
service-accounts
appfabric-cache
graph-drawing
oberon
dundas
device-orientation
offloading
jscript.net
ceil
reactfx
mysql-error-1062
android-2.2-froyo
algebraic-data-types
ccss
apc
jquery-mobile-dialog
amazon-appstore
code-cleanup
fireworks
gnu-prolog
libc++
vdsp
gdlib
lpeg
html-input
gallio
dentrix
icanhaz.js
mozilla-prism
asp.net-profiles
genealogy

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