Php encountered an error when reading xml documents with entity symbols. how can this problem be solved?

Source: Internet
Author: User
Tags processing instruction
Php encountered an error when reading the xml document object symbols, such as & lt; name & gt; aa & amp; nbsp; bb & lt; name & gt; & nbsp; this warning is reported when the entity symbol of this grid exists in the middle (!) Warning: DOMDocument: load () [domdocument. load]: Entitynbspnotdefinedinfile: An error occurred while reading xml documents from xm php.
For example Aa bb
When the entity symbol of this lattice exists in the middle
This warning is reported.
(! ) Warning: DOMDocument: load () [domdocument. load]: Entity 'nbsp 'not defined in file:

Reading xml documents is like this.
$ Xml = new DOMDocument ("1.0", "UTF-8 ");
$ Xml-> load ('myxml. XML ');

------ Solution --------------------
It is good to discover the problem by yourself. since it is not allowed to appear, how are you going to solve it?
------ Solution --------------------
From php Manual
PHP code
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:
  

This is a non-breaking space.

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: ]>

This is a non-breaking space.

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 .,), Which is fig important if you need to do this for each different events Ents with each different possible entities. also, as a sidenote... entity references created by createEntityReference () do not need this kind of explicit declaration.
------ Solution --------------------
Congratulations, lz .~~
Thanks ZT_king
------ Solution --------------------
<> And & cannot appear

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.