When using loadXML() to parse a string that contains entity
references (e.g., ), be sure that those entity
references are properly declared through the use of a
DOCTYPE declaration; otherwise, loadXML() will not be able
to interpret the string.
Example:
<?php
$str = <<<XML
<?xml version="1.0"
encoding="iso-8859-1"?>
<div>This is a non-breaking
space.</div>
XML;
$dd1 = new DOMDocument();
$dd1->loadXML($str);
echo $dd1->saveXML();
?>
Given the above code, PHP will issue a Warning about the
entity 'nbsp' not being properly declared. Also, the call
to saveXML() will return nothing but a trimmed-down version
of the original processing instruction...everything else is
gone, and all because of the undeclared entity.
Instead, explicitly declare the entity first:
<?php
$str = <<<XML
<?xml version="1.0"
encoding="iso-8859-1"?>
<!DOCTYPE root [
<!ENTITY nbsp " ">
]>
<div>This is a non-breaking
space.</div>
XML;
$dd2 = new DOMDocument();
$dd2->loadXML($str);
echo $dd2->saveXML();
?>
Since the 'nbsp' entity is defined in the DOCTYPE, PHP no
longer issues that Warning; the string is now well-formed,
and loadXML() understands it perfectly.
You can also use references to external DTDs in the same way
(e.g., <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
4.01//EN"
"http:
//www.w3.org/TR/html4/strict.dtd">), which is
particularly important if you need to do this for many
different documents with many different possible entities.
Also, as a sidenote...entity references created by
createEntityReference() do not need this kind of explicit
declaration.
----
Server IP: 69.39.81.135
Probable Submitter: 129.120.182.237
----
Manual Page -- http://www.php.net/manual/en/function.dom-domdo
cument-loadxml.php
Edit -- https://master
.php.net/note/edit/74848
Del: integrated -- h
ttps://master.php.net/note/delete/74848/integrated
Del: useless -- http
s://master.php.net/note/delete/74848/useless
Del: bad code -- htt
ps://master.php.net/note/delete/74848/bad+code
Del: spam -- https:/
/master.php.net/note/delete/74848/spam
Del: non-english --
https://master.php.net/note/delete/74848/non-english
Del: in docs -- http
s://master.php.net/note/delete/74848/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/74848
Reject -- https://mast
er.php.net/note/reject/74848
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|