0

I'm creating a XML object using PHP SimpleXMLElement

I have the full XML in $RAWResponse, so I use

$oXMLResponse = new SimpleXMLElement($RAWresponse);

The XML I'm getting comes from a webservice. I detected the XML contains invalid chars, as '&' for instance.

There are a lot of posts about this issue but none works. I tried htmlentities() but this escapes every char, including the '<' and '>' opening and enclosing the XML Elements.

Finally I used this function :

$some_special_chars = array("&");
$replacement_chars  = array("&amp;");
$RAWresponse    = str_replace($some_special_chars, $replacement_chars, $RAWresponse);

$oXMLResponse = new SimpleXMLElement($RAWresponse);

This works but it's a stupid solution, because it will break if I get any other special char. Is there a better solution? I cant understand why SimpleXMLElement breaks, it should only show a warning or something.

4

1 回答 1

1

This is not possible using PHP's built in SimpleXML extension (Or even the DOM extension). You must either replace all invalid characters with their proper entities or request that the web-service provider return valid XML.

于 2013-04-18T23:24:41.017 回答