Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有内部DTD的XML文档类型_Php_Xml - Fatal编程技术网

Php 具有内部DTD的XML文档类型

Php 具有内部DTD的XML文档类型,php,xml,Php,Xml,PHP手册中包含了一个关于如何使用附加DTD声明XML文档类型的示例: <?php // Creates an instance of the DOMImplementation class $imp = new DOMImplementation; // Creates a DOMDocumentType instance $dtd = $imp->createDocumentType('graph', '', 'graph.dtd');

PHP手册中包含了一个关于如何使用附加DTD声明XML文档类型的示例:

    <?php
    // Creates an instance of the DOMImplementation class
    $imp = new DOMImplementation;

    // Creates a DOMDocumentType instance
    $dtd = $imp->createDocumentType('graph', '', 'graph.dtd');

    // Creates a DOMDocument instance
    $dom = $imp->createDocument("", "", $dtd);

    // Set other properties
    $dom->encoding = 'UTF-8';
    $dom->standalone = false;
    [...]
    ?>
更新

通过从包含DTD的现有文件中读取DOCTYPE(无数据)的解决方法:

    $doc = new DOMDocument();
    $doc->load('graph.xml');

    //get the actual root element
    $graph=$doc->documentElement; 

    $graph->appendChild($doc->createElement('id','12'));
    $graph->appendChild($doc->createElement('url','foo.png'));      

    echo $doc->saveXML();
  • 您可以使用(如建议的)将DTD的内容嵌入到文档中
  • 或者,您可以创建一个已包含DTD节点的空文档,然后不使用
    doImplementation
    创建文档,您可以只解析这个预先制作的文档,因为它可以在其属性中包含此类数据,例如:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <!DOCTYPE graph [
      <!ELEMENT graph (id,url)>
      <!ELEMENT id (#PCDATA)>
      <!ELEMENT url (#PCDATA)>
    ]>
    <graph />
    
    
    ]>
    

谢谢您的回答。虽然它没有解释如何使用createDocumentType生成正确的DOCTYPE,但是您关于解析现有文档的建议为我指出了一个解决方案,我现在将使用它。请参阅我的问题的更新。文档中说,您无法使用
createDocumentType()
生成正确的DOCTYPE。它只有以下参数:
qualifiedName
publicId
systemId
。您也不能操作
DOMDocumentType
对象(它只有只读属性)。我想你只有这两个解决办法。
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <!DOCTYPE graph [
      <!ELEMENT graph (id,url)>
      <!ELEMENT id (#PCDATA)>
      <!ELEMENT url (#PCDATA)>
    ]>
    $doc = new DOMDocument();
    $doc->load('graph.xml');

    //get the actual root element
    $graph=$doc->documentElement; 

    $graph->appendChild($doc->createElement('id','12'));
    $graph->appendChild($doc->createElement('url','foo.png'));      

    echo $doc->saveXML();
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE graph [
  <!ELEMENT graph (id,url)>
  <!ELEMENT id (#PCDATA)>
  <!ELEMENT url (#PCDATA)>
]>
<graph />