2014-05-11 2 views
0

XML 파일에 값을 쓰는 PHP 함수 (stackoverflow 멤버가 작성)가 있습니다. XML 요소 중 하나는 CDATA에 포함되어야합니다. 특히 "설명"값. PHP 코드를 실행하면 다음 오류가 발생합니다.DOMDocument를 사용하여 PHP에서 CDATA 구문 분석

Warning: DOMDocument::createElement() expects parameter 2 to be string, object given in /homepages/46/d412048482/htdocs/create_feed.php on line 23

Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given in /homepages/46/d412048482/htdocs/create_feed.php on line 23

여기에서 어디로 가야할 지 모르겠습니다. 누구든지 어떤 제안이 있습니까?

전체 PHP

<?php 

// Script by Fred Fletcher, Canada. 

$title = $_POST['title']; 
$description = $_POST['description']; 

$xml = new DOMDocument('1.0', 'utf-8'); 
$xml->formatOutput = true; 
$xml->preserveWhiteSpace = false; 
$xml->load('rss/dakashmere.xml'); 

$element = $xml->getElementsByTagName('item')->item(0); 

$pubDate = $element->getElementsByTagName('pubDate')->item(0); 
$title = $element->getElementsByTagName('title')->item(0); 
$description = $element->getElementsByTagName('description')->item(0); 

$newItem = $xml->createElement('item'); 

$newItem->appendChild($xml->createElement('title', $_POST['title'])); 
$newItem->appendChild($xml->createElement('description', $xml->createCDATASection($_POST['description']))); 
$newItem->appendChild($xml->createElement('pubDate', date("F j, Y, g:i a",time())));; 

$xml->getElementsByTagName('channel')->item(0)->insertBefore($newItem, $element); 

$xml->save('rss/dakashmere.xml'); 

echo "Data has been written."; 

?> 

이 내가 XML은

<?xml version="1.0" encoding="UTF-8"?> 
<channel> 
    <item> 
    <title>Birthday Bash!</title> 
    <description><![CDATA[ May 3rd, we will be celebrating Team Cherokee's DJ Unique Birthday Bash at Soho Hookah Bar and Lounge. The event will be hosted by Prince Po of Organized Confusion and Honey Dipp. There will be special performances by BITTA Records own Da Kashmere and T Solid. There will also be guest appearances from VH1's Teeny Barrino and Benzino!<br><br><img src="https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/t1.0-9/10177877_440291492772340_8234232488405005533_n.jpg 
" width="50%"> ]]></description> 
    <pubDate>Sat, 3 May 2014 09:00:00 -0500</pubDate> 
    </item> 
</channel> 

답변

2

createCDATASection()처럼 보이게하려는 것입니다있는 XML CDATA 노드의 createElement() 또는 createTextNode() 다른 노드 유형을 생성을 생성 .

당신은 당신의 설명 요소 노드에 추가해야합니다 오류를 제거있어하지만 지금은이 코드에서 자동 이스케이프 문자를 넣어 직면하고있어

$description = $newItem->appendChild($xml->createElement('description')); 
$description->appendChild($xml->createCDATASection($_POST['description'])); 
+0

. "Team Cherokee 's"대신 "Team Cherokee \ 's"및 ""및 " LManer311

+0

DOM은 필요에 따라 CDATA 섹션의 특수 문자를 자동으로 이스케이프합니다. CDATA는 탈출을 필요로하지 않는다는 것을 의미하지는 않습니다. 귀하의 탈출은 magic_quotes_gpc 또는 일부 일반 addslashes에서 발생하는 것으로 보입니다. CDATA 섹션에 추가하기 전에이 문제가 발생합니다. $ _POST 배열을 출력하고 직접보십시오. – ThW

+0

그래, 어디서하는지 알아. 그 일을 막을 수있는 방법이 있습니까? 나는 POST를 출력했고 당신은 옳았다. 문제는 XML을 피드 리더에로드 할 때 추가 이스케이프 문자로 인해 그림이 표시되지 않는다는 것입니다. – LManer311