Adding entities to the DOCTYPE using DomDocument

前端 未结 1 1066
灰色年华
灰色年华 2021-01-22 16:31

I\'m trying to create an XML document that looks something like this...




        
相关标签:
1条回答
  • 2021-01-22 17:08

    The DOM isn't an interface for building document type definitions, which is why you won't find methods for adding things like entity declarations to the internal subset. If you must inline it instead of using an external subset, you're going to have to provide it as a complete string and load it up accordingly.


    Example:

    $xml = <<<'XML'
    <!DOCTYPE stylesheet  [
        <!ENTITY nbsp   "&#160;">
        <!ENTITY copy   "&#169;">
        <!ENTITY reg    "&#174;">
        <!ENTITY trade  "&#8482;">
        <!ENTITY mdash  "&#8212;">
        <!ENTITY ldquo  "&#8220;">
        <!ENTITY rdquo  "&#8221;">
        <!ENTITY pound  "&#163;">
        <!ENTITY yen    "&#165;">
        <!ENTITY euro   "&#8364;">
    ]>
    <NewsPost/>
    XML;
    
    $dom = new DOMDocument();
    $dom->loadXML($xml);
    
    echo $dom->saveXML();
    

    Output:

    <?xml version="1.0"?>
    <!DOCTYPE stylesheet [
    <!ENTITY nbsp "&#160;">
    <!ENTITY copy "&#169;">
    <!ENTITY reg "&#174;">
    <!ENTITY trade "&#8482;">
    <!ENTITY mdash "&#8212;">
    <!ENTITY ldquo "&#8220;">
    <!ENTITY rdquo "&#8221;">
    <!ENTITY pound "&#163;">
    <!ENTITY yen "&#165;">
    <!ENTITY euro "&#8364;">
    ]>
    <NewsPost/>
    
    0 讨论(0)
提交回复
热议问题