0
我有一個腳本生成突然停止工作的XML文檔。當我運行它,我得到這個錯誤:XML分析錯誤...找不到元素
XML解析錯誤:沒有的元素中找到 地點:http://www.mydomain.com/indeed-general-xml/ 1號線,1列:
下面是生成節點結構的代碼和循環:
header('Content-Type: text/xml');
//create DOMDocument object and set char encoding
$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
//root element
$r = $doc->createElement("source");
//append root element to our document
$doc->appendChild($r);
$publisher = $doc->createElement("publisher");
$publisher->appendChild($doc->createTextNode("mydomain.com"));
$r->appendChild($publisher);
$publisherurl = $doc->createElement("publisherurl");
$publisherurl->appendChild($doc->createTextNode("http://mydomain.com"));
$r->appendChild($publisherurl);
$job = array();
//set args for query
$args = array(
'post_author' => -1,
'post_type' => 'job_listing',
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
);
//run query
$feed_jobs = new WP_Query($args);while($feed_jobs->have_posts()) : $feed_jobs->the_post();
$b = $doc->createElement("job");
//begin loop
(while $feed_jobs->have_posts()) : $feed_jobs->the_post();
$title = $doc->createElement("title");
$title->appendChild($doc->createCDATASection($post->post_title));
$b->appendChild($title);
$company_name = "Sample Company Name";
$company = $doc->createElement("company");
$company->appendChild($doc->createCDATASection($company_name));
$b->appendChild($company);
$date = $doc->createElement("date");
$date->appendChild($doc->createCDATASection($post->post_date));
$b->appendChild($date);
$referencenumber = $doc->createElement("referencenumber");
$referencenumber->appendChild($doc->createCDATASection($post->ID));
$b->appendChild($referencenumber);
$url = $doc->createElement("url");
$url->appendChild($doc->createCDATASection($post->guid));
$b->appendChild($url);
$description = $doc->createElement("description");
$description = createCDATASection($post->post_content);
$description->appendChild($description);
$b->appendChild($description);
//query postmeta table for city and state// then parse it into an array
$location = get_post_meta($post->ID,'geo_address',true);
$location = explode(',',$location);
$city = $doc->createElement("city");
$city->appendChild($doc->createCDATASection($location[0]));
$b->appendChild($city);
$state = $doc->createElement("state");
$state->appendChild($doc->createCDATASection($location[1]));
$b->appendChild($state);
$r->appendChild($b);
endwhile;
我不認爲這是我的標題中的空行問題,因爲我有一個完全相同的腳本,唯一不同的是從數據庫中提取的用戶數據。當我將查詢交換出來時,破損的腳本沒有任何問題。
這個錯誤讓我瘋狂。非常感謝您的幫助。
編輯:這裏是預期的輸出的一個示例我被要求爲
<source>
<publisher>mydomain.com</publisher>
<publisherurl>http://mydomain.com</publisherurl>
<job>
<company_name>ABC Company</company_name>
<date>3-4-2011 10:54</date>
<referencenumber>1234</referencenumber>
<url>http://www.mydomain.com/extension</url>
<description>Block of of content, likely ranging several thousand characters. HTML tags included, but should be sanitized by use of createCDATASection() method</description>
<city>Oakland</city>
<state>California</state>
</job>
</source>
XML文檔是什麼樣的? – 2012-02-10 18:19:56
像什麼?我想你錯過了一些東西:) – 2012-02-10 19:27:28
是的,我試圖編輯我的評論,但它超時。對不起,這裏的註釋格式不利於打印出結構良好的XML文檔。儘管通過查看代碼,您應該可以看到,,,的樹結構。您無法從代碼中查看的唯一一件事是正確打開和關閉的根節點