2017-03-09 1 views
0

Google 쇼핑을위한 XML 문서를 만들고있어 제품 설명의 인코딩에 문제가 있습니다. str_replace으로 다른 접근 방식을 시도하고 일부 문자 인 utf8_encode, iconv 및 일부 맞춤 기능을 이스케이프하지만 생성 된 인코딩 오류는 모두 발생했습니다. 그렇다면 createTextNode을 사용하는 사람이 나를 위해 일하는 것처럼 보였습니다. 오류가 없습니다.PHP DOMDocument 네임 스페이스가있는 createTextNode

내가 지금 가지고있는 유일한 문제는 오른쪽 네임 스페이스에 createTextNode을 얻을 수 없다는 것입니다. (나는 올바른 길을 말하고 싶습니다.)

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

이 오른쪽 라인을 생성합니다 :

<description>test content</description> 

지금 나는 createTextNode 코드를 사용하려면를

때문에 인코딩 문제에 근무하지만, 일부 제품 설명에 실패 코드입니다 하지만 올바른 태그로 작업 할 수는 없습니다. 작동 방식 :

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

그러나 설명 태그 사이에 있어야하지만 내용은 내 메인 항목에 저장됩니다.

어떻게 설명 태그에 넣을 수 있습니까? 또는 이전 코드를 사용하는 동안 인코딩 문제를 해결하는 좋은 방법을 알고 있다면 괜찮습니다.


여기 내가 사용 전체 코드입니다 :

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

문자 노드는 네임 스페이스가없는 노드. 여기에는 두 가지 유형이 있습니다. 특수 구문을 사용하는 XML 특수 문자 및 CDATA 섹션을 인코딩하는 텍스트 노드입니다. 두 가지 모두 atom:descriptionatom:summary에 사용할 수 있습니다. 예상 내용 (Atom Parser의 경우)은 type 속성에 따라 다릅니다.

기본값은 text입니다. html은 XHTML 네임 스페이스의 자식 노드 인 텍스트로 인코딩 된 HTML 조각을 예상한다는 것을 의미합니다. 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

관련 문제