2017-04-07 61 views
0

我用PHP創建XML文件時,這是輸出我得到編碼錯誤節約用PHP(HTML實體)XML文檔

error on line 2 at column 165: Encoding error 
Below is a rendering of the page up to the first error. 

在源我只看到這個

<?xml version="1.0" encoding="UTF-8"?> 
<ad_list><ad_item class="class"><description_raw>Lorem ipsum dolor sit amet &#13; 
&#13; 
&#13; 
&#13; 

這是我使用的代碼(strip_shortcodes是用於刪除Wordpress使用的標籤的Wordpress功能)。

$xml = new DOMDocument('1.0', 'UTF-8'); 
$xml__item = $xml->createElement("ad_list");  

$ad_item = $xml->createElement("ad_item"); 
$xml__item->appendChild($ad_item); 

$description_raw = $xml->createElement("description_raw", strip_shortcodes(html_entity_decode(get_the_content()))); 
$ad_item->appendChild($description_raw); 

$xml->appendChild($xml__item); 
$xml->save($filename); 

如果我從description_raw刪除html_entity_decode功能比全時生成的XML但當時我有這個錯誤

error on line 6 at column 7: Entity 'nbsp' not defined 
Below is a rendering of the page up to the first error. 
+1

'' 非XML有效的實體,對其進行解碼纔是正道。但是你使用了'DOMDocument :: createElement()'的第二個參數。這是破碎的:http://stackoverflow.com/questions/22956330/cakephp-xml-utility-library-triggers-domdocument-warning/22957785#22957785 – ThW

回答

0

這是極不可能的,你得到的值將是有效的在一個XML文件。您應該將其添加爲CDATA部分。嘗試是這樣的:

<?php 
$xml = new DOMDocument('1.0', 'UTF-8'); 
$xml__item = $xml->createElement("ad_list");  

$ad_item = $xml->createElement("ad_item"); 
$xml__item->appendChild($ad_item); 

$description_raw = $xml->createElement("description_raw"); 
$cdata = $xml->createCDATASection(get_the_content()); 
$description_raw->appendChild($cdata); 
$ad_item->appendChild($description_raw); 

$xml->appendChild($xml__item); 
$xml->save($filename); 

或者,只是建立它自己:

<?php 
$content = get_the_content(); 
$xml = <<< XML 
<?xml version="1.0"?> 
<ad_list> 
    <ad_item> 
     <description_raw><![CDATA[ $content ]]></description_raw> 
    </ad_item> 
</ad_list> 

XML; 
file_put_contents($filename, $xml);