How to import XML string in a php DOMDocument

前端 未结 2 1507
梦毁少年i
梦毁少年i 2020-12-09 13:32

For exemple, i create a DOMDocument like that :

createDocumen         


        
相关标签:
2条回答
  • 2020-12-09 14:00

    The problem is DOM does not know that it should consider the XHTML DTD unless you validated the document against it. Unless you do that, DOM doesnt know any entities defined in the DTD, nor any other rules in it. Fortunately, we sorted out how to do the validation in that other question, so armed with that knowledge you can do

    $document->validate(); // anywhere before importing the other DOM
    

    And then import with

    $fragment = $document->createDocumentFragment();
    $fragment->appendXML('<h1>Hello</h1><p>Hello&nbsp;World</p>');
    $document->getElementsByTagName('body')->item(0)->appendChild($fragment);
    $document->formatOutput = TRUE;
    echo $document->saveXml();
    

    outputs:

    <?xml version="1.0"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>My bweb page</title>
      </head>
      <body>
        <h1>Hello</h1>
        <p>Hello&nbsp;World</p>
      </body>
    </html>
    

    The other way to import XML into another DOM is to use

    $one = new DOMDocument;
    $two = new DOMDocument;
    $one->loadXml('<root><foo>one</foo></root>');
    $two->loadXml('<root><bar><sub>two</sub></bar></root>');
    $bar = $two->documentElement->firstChild; // we want to import the bar tree
    $one->documentElement->appendChild($one->importNode($bar, TRUE));
    echo $one->saveXml();
    

    outputs:

    <?xml version="1.0"?>
    <root><foo>one</foo><bar><sub>two</sub></bar></root>
    

    However, this cannot work with

    <h1>Hello</h1><p>Hello&nbsp;World</p>
    

    because when you load a document into DOM, DOM will overwrite everything you told it before about the document. Thus, when using load, libxml (and thus SimpleXml, DOM and XMLReader) does (do) not know you mean XHTML. And it does not know any entities defined in it and will fuzz about them instead. But even if the string would not contain the entity, it is not valid XML, because it lacks a root node. That's why you use the fragment.

    0 讨论(0)
  • 2020-12-09 14:09

    You can use a DomDocumentFragment for this:

    $fragment = $document->createDocumentFragment();
    $fragment->appendXml($xhtml);
    $elementBody->appendChild($fragment);
    

    That's all there is to it...

    Edit: Well, if you must have xhtml (instead of valid xml), you could do this dirty workaround:

    function xhtmlToDomNode($xhtml) {
        $dom = new DomDocument();
        $dom->loadHtml('<html><body>'.$xhtml.'</body></html>');
        $fragment = $dom->createDocumentFragment();
        $body = $dom->getElementByTagName('body')->item(0);
        foreach ($body->childNodes as $child) {
            $fragment->appendChild($child);
        }
        return $fragment;
    }
    

    usage:

    $fragment = xhtmlToDomNode($xhtml);
    $document->importNode($fragment, true);
    $elementBody->appendChild($fragment);
    
    0 讨论(0)
提交回复
热议问题