2017-03-09 10 views
0

私はGoogleショッピング用のXML文書を作成しており、商品説明のエンコーディングに問題がありました。 str_replaceでいくつかの文字をエスケープしようとしましたが、utf8_encodeiconvといくつかのカスタム関数が生成されましたが、すべてのエンコーディングエラーが発生しました。その後、私はcreateTextNodeを使用して、それは私のために動作するように見えた誰かを見つけた、私はエラーが出なかった。PHP DOMDocument名前空間を持つcreateTextNode

私が今得た唯一の問題は、右の名前空間にcreateTextNodeを得ることができないことです(正しい方法を言ってほしいと思います)。

これは働いていたコードですが、原因エンコードの問題のために、いくつかの製品の説明に失敗しました:

<description>test content</description> 

は、今私はcreateTextNodeコードを使用する:

$addProduct->appendChild($domtree->createElementNS($xmlns['atom'], 'description', 'test content')); 

これは、右の行を作成します正しいタグで作業することはできません。これは動作します:

$addProduct->appendChild($domtree->createTextNode('test content')); 

しかし、これはdescriptionタグの間にある必要がありますが、これは私のメインエントリに内容を置きます。

どのように説明タグに入れることができますか? また、古いコードを使用しているときにエンコードの問題を解決する良い方法が分かっていれば、それも問題ありません。


はここで私が使用している全体のコードです:

function function_xml_entities($text = null, $charset = 'ISO-8859-1'){ 
    $text = htmlentities($text, ENT_COMPAT, $charset, false); 
    $arr_xml_special_char = array("&quot;","&amp;","&apos;","&lt;","&gt;"); 
    $arr_xml_special_char_regex = "(?"; 
    foreach($arr_xml_special_char as $key => $value){ 
     $arr_xml_special_char_regex .= "(?!$value)"; 
    } 
    $arr_xml_special_char_regex .= ")"; 
    $pattern = "/$arr_xml_special_char_regex&([a-zA-Z0-9]+;)/"; 
    $replacement = '&amp;${1}'; 
    return preg_replace($pattern, $replacement, $text); 
} 

function function_html2text($html = null){ 
    $tags = array (
     0 => '~<h[123][^>]+>~si', 
     1 => '~<h[456][^>]+>~si', 
     2 => '~<table[^>]+>~si', 
     3 => '~<tr[^>]+>~si', 
     4 => '~<li[^>]+>~si', 
     5 => '~<br[^>]+>~si', 
     6 => '~<p[^>]+>~si', 
     7 => '~<div[^>]+>~si', 
    ); 
    $html = preg_replace($tags,"\n",$html); 
    $html = preg_replace('~</t(d|h)>\s*<t(d|h)[^>]+>~si',' - ',$html); 
    $html = preg_replace('~<[^>]+>~s','',$html); 
    // reducing spaces 
    $html = preg_replace('~ +~s',' ',$html); 
    $html = preg_replace('~^\s+~m','',$html); 
    $html = preg_replace('~\s+$~m','',$html); 
    // reducing newlines 
    $html = preg_replace('~\n+~s',"\n",$html); 
    return $html; 
} 

$sql_products = "QUERY WHICH IS NOT RELEVANT"; 
$result_products = mysql_query($sql_products); 

//create a dom document with encoding utf8 
$domtree = new DOMDocument('1.0', 'UTF-8'); 

//create the root element of the xml tree 
$xmlRoot = $domtree->createElement("feed"); 
$xmlRoot = $domtree->appendChild($xmlRoot); 

//create a dom document with encoding utf8 
$domtree = new DOMDocument('1.0', 'UTF-8'); 

//create the root element of the xml tree 
$xmlns = array('atom' => 'http://www.w3.org/2005/Atom','g' =>'http://base.google.com/ns/1.0'); 
$xmlRoot = $domtree->appendChild($domtree->createElementNS($xmlns['atom'], 'feed')); 
$xmlRoot->setAttributeNS($xmlns['g'], 'g:dummy', ''); //add a dummy attribute to add the google namespace to the document element 
$xmlRoot->removeAttribute('g:dummy'); //remove dummy attribute 

//Standard things like title 
$xmlRoot->appendChild($domtree->createElement('title', 'title')); 
$link = $xmlRoot->appendChild($domtree->createElement('link')); 
$link->setAttribute('rel', 'self'); 
$link->setAttribute('href', $global_websitenaam_include); 
$xmlRoot->appendChild($domtree->createElement('updated', date('Y-m-d H:i:s'))); 
$addAuthor = $xmlRoot->appendChild($domtree->createElement("author")); 
    $addAuthor->appendChild($domtree->createElement('name', 'author name')); 
$xmlRoot->appendChild($domtree->createElement('id', 'tag:website.com,'.date('Y-m-d'))); 

//Producten doorlopen 
while($product = mysql_fetch_assoc($result_products)){ 
    //HERE ARE OTHER QUERIES AND DEFINING VARIABLES WHICH AREN'T RELEVANT TO THE CODE 
    $product_content = function_xml_entities(substr_replace(str_replace('&nbsp;',' ', function_html2text($product['content'])), "", 5000)); 

    // create the products 
    $addProduct = $xmlRoot->appendChild($domtree->createElementNS($xmlns['atom'], "entry")); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['atom'], 'id', $product['id'])); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['atom'], 'title', substr_replace($product['name'], "", 150))); 
    $linkProd = $addProduct->appendChild($domtree->createElement('link')); 
     $linkProd->setAttribute('href', $global_websitenaam_include.'/'.rawurlencode($product['category_slug']).'/'.rawurlencode($product['slug'])); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:price', number_format($product_price, 2, ',', '.'))); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:condition', $condition_product)); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:brand', substr_replace($product['manufacturer_name'], "", 70))); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:mpn', $product['typenumber'])); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:ean', $product['ean'])); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:image_link', $product_image)); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:product_type', 'Huis &amp; Tuin &gt; '.$parentcategory_name.$product['category_name'])); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:availability', $product_stock)); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:manufacturer', $product['supplier_name'])); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:weight', $product['weight'])); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:featured_product', $product_advertisement)); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['g'], 'g:size', $product['size'])); 
    $addProductShipping = $addProduct->appendChild($domtree->createElement("g:shipping")); 
     $addProductShipping->appendChild($domtree->createElement('g:country', 'NL')); 
     $addProductShipping->appendChild($domtree->createElement('g:service', 'Standaard')); 
     $addProductShipping->appendChild($domtree->createElement('g:price', number_format($shipment_price, 2, ',', '.'))); 
    $addProduct->appendChild($domtree->createElementNS($xmlns['atom'], 'description', $product_content)); 

    //$addProduct->appendChild($domtree->createTextNode($product_content)); 
} 

//get the xml printed 
header("content-type: text/xml; charset: utf-8"); 
$domtree->formatOutput = true; 
echo $domtree->saveXML();  

答えて

1

キャラクターのノードが名前空間を持たないノード。ここには2つのタイプがあります。特別な構文を使用するXML特殊文字とCDATAセクションをエンコードするテキストノード。どちらもatom:descriptionatom:summaryに使用できます。予想される内容(Atom Parserの場合)はtype属性に依存します。

デフォルトはちょうどtextです。htmlは、HTMLフラグメントがテキストとしてエンコードされていることを意味します。xhtmlはXHTML名前空間の子ノードです。

createElement()/createElementNS()というコンテンツ引数を使用しないでください。または、ここに値(空文字列、整数など)がないことが確実な場合を除いて、$ nodeValueプロパティを設定しないでください。彼らは壊れたエンコーディングを使用します。 DOMDocument::createTextNode()またはDOMDocument::createCDATASection()を使用して文字ノードを作成します。ここで

は小さな一例です:

$xmlns = [ 
    'atom' => 'http://www.w3.org/2005/Atom' 
]; 
$htmlFragment = '<div>Description HTML Fragment</div>'; 

$document = new DOMDocument(); 
$entry = $document->appendChild(
    $document->createElementNS($xmlns['atom'], 'entry') 
); 
$summary = $entry->appendChild(
    $document->createElementNS($xmlns['atom'], 'summary') 
); 
$summary->setAttribute('type', 'text'); 
$summary->appendChild(
    $document->createTextNode('Summary Text') 
); 
$description = $entry->appendChild(
    $document->createElementNS($xmlns['atom'], 'description') 
); 
$description->setAttribute('type', 'html'); 
$description->appendChild(
    $document->createCDATASection($htmlFragment) 
); 

$document->formatOutput = TRUE; 
echo $document->saveXml(); 

出力:精巧な答えを

<?xml version="1.0"?> 
<entry xmlns="http://www.w3.org/2005/Atom"> 
    <summary type="text">Summary Text</summary> 
    <description type="html"><![CDATA[<div>Description HTML Fragment</div>]]></description> 
</entry> 
+0

ありがとう!それは私がdomdocument構造をもっとよく理解するのを助けました。 – Femke