2016-03-08 68 views
2

在PHP中我使用DOMDocument創建我的XML:加入DOCTYPE在XML與PHP的DOMDocument

$xml_data = new DOMDocument("1.0", "UTF-8"); 

// create root element 
$root = $xml_data->createElement("cXML"); 
$xml_data->appendChild($root); 

etcetera 

是否有可能加入這個元素:與PHP的DOMDocument

<!DOCTYPE cXML 
    SYSTEM 'http://xml.cXML.org/schemas/cXML/1.2.008/cXML.dtd'> 

到XML,等你拿:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE cXML 
    SYSTEM 'http://xml.cXML.org/schemas/cXML/1.2.008/cXML.dtd'> 

回答

0

您可以使用DOMImplementation代替:

$imp = new DOMImplementation(); 
$xml_data = $imp->createDocument(
    null, 
    'cXML', 
    $imp->createDocumentType('cXML', 
     null, 
     'http://xml.cXML.org/schemas/cXML/1.2.008/cXML.dtd' 
    ) 
); 
$xml_data->xmlVersion="1.0"; 
$xml_data->encoding="UTF-8"; 

// No need to create root elemet, it's already there 
// $root = $xml_data->createElement("cXML"); 
// $xml_data->appendChild($root); 

etcetera