php讀取xml文檔遇到實體符號時出錯,該如何解決

來源:互聯網
上載者:User
php讀取xml文檔遇到實體符號時出錯
如aa bb
中間有這這格的實體符號時
會報出這種警告
( ! ) Warning: DOMDocument::load() [domdocument.load]: Entity 'nbsp' not defined in file:

讀取xml文檔是這樣子
$xml = new DOMDocument("1.0","UTF-8");
$xml->load('myxml.xml');

------解決方案--------------------
能自己發現問題就好,既然不允許出現,你準備怎麼解決呢?
------解決方案--------------------
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 particularly important if you need to do this for many different documents with many different possible entities.Also, as a sidenote...entity references created by createEntityReference() do not need this kind of explicit declaration.
------解決方案--------------------
恭喜lz。~~
thanks ZT_king
------解決方案--------------------
< > 和 & 這三個不能出現
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.