开发者

xml parsing of special characters, such as '&' symbol in php

I am having trouble to parse special characters from an xml file (I can't edit the file) with special characters, such as the '&' symbol in php.

Here is an example xml code snippet that I try to make it work and display in php.

<?xml version="1.0" encoding="ISO开发者_JS百科-8859-1"?> <node> <test>Don't & forget me this weekend!</test> </node>

Any help is highly appreciated :)


To use the & special character the code is &amp;

Also have a look at : http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references


In that one corner case:

str_replace("&", "&amp;", $xml);

Would make it parse properly. Unfortunately if you have a great than or less than sign unescaped:

<?xml version="1.0" encoding="ISO-8859-1"?>
<node>
  <test>Oh my < Goodness </test>
</node>

A str_replace is not going work. Now you could do regex to test something like <[a-zA-Z], but then there's:

<?xml version="1.0" encoding="ISO-8859-1"?>
<node>
  <test>Oh my<Goodness </test>
</node>

So that will basically kill your chances of using a regex.

I've tried to parse your XML with SimpleXML, DOM, XmlReader, and attempted a number of libxml properties, and nothing worked.

So in short, the odds are against you. Your provider needs to generate proper XML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜