使用php domdocument在xml中添加doctype

时间:2016-03-08 11:29:33

标签: php xml domdocument

在php中我使用domdocument创建我的XML:

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

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

etcetera

是否可以添加此元素:

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

使用PHP domdocument进入XML,所以得到:

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

1 个答案:

答案 0 :(得分:1)

您可以改为使用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