i have a problem to load a xml-file with php. I use DOMDocument, because i need the function getElementsByTagName
.
I use this code.
$dom = new DomDocument('1.0', 'UTF-8');
$dom->resolveExternals = false;
$dom->load($_FILES["file"]["tmp_name"]);
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<value>1796563</value>
<value>Verliebt! ’</value>
</Data>
ErrorMessage:
Warning: DOMDocument::load() [domdocument.load]: Entity 'rsquo' not defined in /tmp/php1VRb3N, line: 4 in /www/htdocs/bla/upload.php on line 51
Your XML parser is not lying. That's an invalid (not even well-formed) document that you won't be able to load with anything.
rsquo
is a predefined entity in HTML, but not in XML. In an XML document if you want to use anything but the most basic built-in entities (amp
,lt
,gt
,quot
andapos
) you must define them in a DTD referenced by a<!DOCTYPE>
declaration. (This is how XHTML does it.)You need to find out where the input came from and fix whatever was responsible for creating it, because at the moment it's simply not XML. Use a character reference (
’
) or just the plain literal character’
in UTF-8 encoding.As a last resort if you really have to accept this malformed nonsense for input you could do nasty string replacements over the file:
If you need to do this for all the non-XML HTML entities and not just
rsquo
that's a bit more tricky. You could do a regex replacement:This still isn't great as it's going to be mauling any sequences of
&\w+;
characters inside places like comments, CDATA sections and PIs, where this doesn't actually mean an entity reference. But it's probably about the best you can do given this broken input.It's certainly better than calling
html_entity_decode
over the whole document, which will also mess up any XML entity references, causing the document to break whenever there's an existing&
or<
.Another hack, questionable in different ways, would be to load the document using
loadHTML()
.