2017-04-07 5 views
0

私はPHPでのXMLファイルを作成しています、これは私がこれだけエンコーディング・エラー(HTMLエンティティ)

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

を参照してくださいソースで

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

を取得し、出力されますこれは私が使用しているコードです(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); 

私は完全なXMLが生成されているよりもdescription_rawからhtml_entity_decode機能を削除したが、その後、私はこのエラーを持っている

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()'の2番目の引数を使っています。これは壊れています: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); 
関連する問題