Reputation: 3103
I am creating an XML file from scratch using PHP's SimpleXML Object. But I am getting this error when validating the document: 'Can not find declaration of element "example".'
Here is the code that is creating the XML document:
$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);
The XML that is produced is this:
<?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>
Any help would be appreciated! Thanks
Upvotes: 0
Views: 3827
Reputation: 287915
In order to validate an XML document, you need a DTD (or XML Schema) which describes how a valid document looks like. You need write a DTD example.dtd
for your XML application, and either give it to the validator or include it in your XML document, by prefixing it with
<!DOCTYPE example SYSTEM "example.dtd">
Since SimpleXML does not support doctypes, you must either manually prefix the above line or use php's DOM extension. Fortunately, you can import the SimpleXML fragment to DOM with dom_import_simplexml
.
Upvotes: 1
Reputation: 336258
An XML file needs something to validate it against, a Document Type Definition (DTD) or an XML Schema. You're not providing any, so validation (i.e., checking if the structure/content of the XML document conforms to the rules set forth in the DTD/Schema) is impossible.
Or did you just wish to check for well-formedness (i.e., checking that all tags are closed properly, that there are no illegal characters anywhere etc.)?
Upvotes: 2