I tried created an XML file using below code.
$xml = new SimpleXMLElement('<xml />');
while($rs = db_fetch_object($d_res)){
$cmp = $xml->addChild('Company');
$cmp->addChild('ID', $rs->nid);
$cmp->addChild('Name', trim($rs->title));
$cmp->addChild('Created', $rs->created);
$cmp->addChild('Updated', $rs->changed);
}
header("Content-type: text/xml;charset=utf-8;");
print($xml->asXML());
But when I try to validate it I am getting below warnings
- No DOCTYPE found! Checking XML syntax only.
- No Character encoding declared at document level.
How to clear them? The XML is here and the validator used is validator.w3.org.
Although these are warnings that you could possibly ignore, it is indeed a good idea to try to fix them.
The first message indicates that the xml file doesn’t specify any DTD; the second one refers to the fact that the xml directive doesn’t contain any encoding attribute.
Unfortunately, from looking at the simplexml API, it doesn’t look like it is possible to add either (although I might be missing something, not being familiar with it). I guess being “simple,” it is perfect for quickly read an xml file, but no so much to produce xml. You’ll probaby have to turn to something slightly “heavier” like XML_Serializer.