XML Parsing Error: undefined entity - special characters

前提是你 提交于 2020-01-02 02:48:05

问题


Why does XML display error on certain special characters and some are ok?

For instance, below will create error,

<?xml version="1.0" standalone="yes"?>
<Customers>
    <Customer>
        <Name>L&ouml;ic</Name>
    </Customer>
</Customers>

but this is ok,

<?xml version="1.0" standalone="yes"?>
<Customers>
    <Customer>
        <Name>&amp;</Name>
    </Customer>
</Customers>

I convert the special character through php - htmlentities('Löic',ENT_QUOTES) by the way.

How can I get around this?

Thanks.

EDIT:

I found that it works fine if I use numeric character such as L&#243;ic

now I have to find how to use php to convert special characters into numeric characters!


回答1:


There are five entities defined in the XML specification — &amp;, &lt;, &gt;, &apos; and &quot;

There are lots of entities defined in the HTML DTD.

You can't use the ones from HTML in generic XML.

You could use numeric references, but you would probably be better off just getting your character encodings straight (which basically boils down to:

  • Set your editor to save the data in UTF-8
  • If you process the data with a programming language, make sure it is UTF-8 aware
  • If you store the data in a database, make sure it is configured for UTF-8
  • When you serve up your document, make sure the HTTP headers specify that it is UTF-8 (in the case of XML, UTF-8 is the default, so not specifying anything is almost as good)

)




回答2:


Because it is not an built-in entity, it is instead an external entity that needs declaration in DTD.



来源:https://stackoverflow.com/questions/4787700/xml-parsing-error-undefined-entity-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!