我有一个脚本生成一个突然停止工作的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>
答案 0 :(得分:0)
$description = $doc->createElement("description");
$description = createCDATASection($post->post_content);
$description->appendChild($description);
???
你的意思是为自己添加一些东西吗?