我使用PHP的SimpleXML對象從頭開始創建XML文件。但在驗證文檔時出現此錯誤:'無法找到元素聲明「示例」。「XML驗證錯誤:'無法找到元素聲明「示例」。「
下面是創建XML文檔代碼:
$xml = new SimpleXMLElement('<example></example>');
$xml->addChild('producers');
foreach($producers as $i=>$producer){
$name = get_the_title($producer->ID);
$owner = get_post_meta($producer->ID, '_producer_contact_name', true);
$phone = get_post_meta($producer->ID, '_producer_phone', true);
$fax = get_post_meta($producer->ID, '_producer_fax', true);
$email = get_post_meta($producer->ID, '_producer_email', true);
$website = get_post_meta($producer->ID, '_producer_website', true);
$address = get_post_meta($producer->ID, '_producer_address', true);
$xml->producers->addChild('producer');
$xml->producers->producer[$i]->addChild('name', $name);
$xml->producers->producer[$i]->addChild('owner', $owner);
$xml->producers->producer[$i]->addChild('phone', $phone);
$xml->producers->producer[$i]->addChild('fax', $fax);
$xml->producers->producer[$i]->addChild('email', $email);
$xml->producers->producer[$i]->addChild('website', $website);
$xml->producers->producer[$i]->addChild('civic', $address[0]);
$xml->producers->producer[$i]->addChild('mailing', $address[1]);
$xml->producers->producer[$i]->addChild('town', $address[2]);
$xml->producers->producer[$i]->addChild('province', $address[3]);
$xml->producers->producer[$i]->addChild('postal', $address[4]);
}
$open = fopen($file, 'w') or die ("File cannot be opened.");
fwrite($open, $xml->asXML());
fclose($open);
所產生的XML是這樣的:
<?xml version="1.0"?>
<example>
<producers>
<producer>
<name></name>
<phone></phone>
<fax></fax>
<email></email>
<website></website>
<civic></civic>
<mailing></mailing>
<town></town>
<province></province>
<postal></postal>
</producer>
</producers>
</example>
任何幫助,將不勝感激!謝謝
你如何驗證它? – Gordon
可能重複[無法找到元素'作業'的聲明](http://stackoverflow.com/questions/1449797/cannot-find-the-declaration-of-element-assignments) – Gordon