drh96824 2014-06-07 14:06
浏览 128
已采纳

如何区分DOMDocument中的空元素和空大小的字符串?

I have trouble to load XML document into DOM preserving empty tags and null-size strings. Here the example:

$doc = new DOMDocument("1.0", "utf-8");

$root = $doc->createElement("root");
$doc->appendChild($root);

$element = $doc->createElement("element");
$root->appendChild($element);

echo $doc->saveXML();

produces following XML:

<?xml version="1.0" encoding="utf-8"?>
<root><element/></root>

Empty element, exactly as expected. Now let's add empty text node into element.

$doc = new DOMDocument("1.0", "utf-8");

$root = $doc->createElement("root");
$doc->appendChild($root);

$element = $doc->createElement("element");
$element->appendChild($doc->createTextNode(""));
$root->appendChild($element);

echo $doc->saveXML();

produces following XML:

<?xml version="1.0" encoding="utf-8"?>
<root><element></element></root>

Non-empty element with null-size string. Good! But when I am trying to do:

$doc = new DOMDocument();
$doc->loadXML($xml);

echo $doc->saveXML($doc);

on these XML documents I always get

<?xml version="1.0" encoding="utf-8"?>
<root><element/></root>

ie null-size string is removed and just empty element is loaded. I believe it happens on loadXML(). Is there any way to convince DOMDocument loadXML() not to convert null-size string into empty element? It would be preferable if DOM would have TextNode with null-size string as element's child.

Solution is needed to be in PHP DOM due to the way what would happen to the loaded data further.

  • 写回答

3条回答 默认 最新

  • douzhuo6931 2014-06-08 19:04
    关注

    The problem to distinguish between those two is, that when DOMDocument loads the XML serialized document, it does only follow the specs.

    By the book, in <element></element> there is no empty text-node in that element - which is what others have commented already as well.

    However DOMDocument is perfectly fine if you insert an empty text-node there your own. Then you can easily distinguish between a self-closing tag (no children) and an empty element (having one child, an empty text-node).

    So how to enter those empty text-nodes? For example by using from the XMLReader based XMLReaderIterator library, specifically the DOMReadingIteration, which is able to build up the document, while offering each current XMLReader node for interaction:

    $doc = new DOMDocument();
    
    $iterator = new DOMReadingIteration($doc, $reader);
    
    foreach ($iterator as $index => $value) {
        // Preserve empty elements as non-self-closing by making them non-empty with a single text-node
        // children that has zero-length text
        if ($iterator->isEndElementOfEmptyElement()) {
            $iterator->getLastNode()->appendChild(new DOMText(''));
        }
    }
    
    echo $doc->saveXML();
    

    That gives for your input:

    <?xml version="1.0" encoding="utf-8"?>
    <root><element></element></root>
    

    This output:

    <?xml version="1.0"?>
    <root><element></element></root>
    

    No strings attached. A fine build DOMDocument. The example is from examples/read-into-dom.php and a fine proof that it is no problem when you load the document via XMLReader and you deal with that single special case you have.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。