【问题标题】:DOMDocument::loadXML() errorDOMDocument::loadXML() 错误
【发布时间】:2011-08-03 13:33:17
【问题描述】:

当我在 PHP 文件中使用 DOMDocument 时,出现以下错误:

警告:DOMDocument::loadXML() [domdocument.loadxml]:Entity 中标记节点第 7 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行

警告:DOMDocument::loadXML() [domdocument.loadxml]:Entity 中标记节点第 6 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行

警告:DOMDocument::loadXML() [domdocument.loadxml]:Entity 中标记字段第 5 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行

警告:DOMDocument::loadXML() [domdocument.loadxml]:Entity 中标记字段第 5 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行

警告:DOMDocument::loadXML() [domdocument.loadxml]: Entity 中标签字段第 4 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行

警告:DOMDocument::loadXML() [domdocument.loadxml]: Entity 中标签字段第 4 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行

警告:DOMDocument::loadXML() [domdocument.loadxml]:Entity 中标记节点第 3 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行

警告:DOMDocument::loadXML() [domdocument.loadxml]:Entity 中标记节点第 2 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行

警告:DOMDocument::loadXML() [domdocument.loadxml]:Entity 中标记节点第 2 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行

警告:DOMDocument::loadXML() [domdocument.loadxml]: Entity 中标记节点第 1 行中的数据过早结束,第 77 行 D:\wamp\www\dom1.php 中的第 7 行 数组(0){}

php代码如下:

<?php
class MyDOMDocument extends DOMDocument
{
    public function toArray(DOMNode $oDomNode = null)
    {
        // return empty array if dom is blank
        if (is_null($oDomNode) && !$this->hasChildNodes()) {
            return array();
        }
        $oDomNode = (is_null($oDomNode)) ? $this->documentElement : $oDomNode;
        if (!$oDomNode->hasChildNodes()) {
            $mResult = $oDomNode->nodeValue;
        } else {
            $mResult = array();
            foreach ($oDomNode->childNodes as $oChildNode) {
                // how many of these child nodes do we have?
                // this will give us a clue as to what the result structure should be
                $oChildNodeList = $oDomNode->getElementsByTagName($oChildNode->nodeName); 
                $iChildCount = 0;
                // there are x number of childs in this node that have the same tag name
                // however, we are only interested in the # of siblings with the same tag name
                foreach ($oChildNodeList as $oNode) {
                    if ($oNode->parentNode->isSameNode($oChildNode->parentNode)) {
                        $iChildCount++;
                    }
                }
                $mValue = $this->toArray($oChildNode);
                $sKey   = ($oChildNode->nodeName{0} == '#') ? 0 : $oChildNode->nodeName;
                $mValue = is_array($mValue) ? $mValue[$oChildNode->nodeName] : $mValue;
                // how many of thse child nodes do we have?
                if ($iChildCount > 1) {  // more than 1 child - make numeric array
                    $mResult[$sKey][] = $mValue;
                } else {
                    $mResult[$sKey] = $mValue;
                }
            }
            // if the child is <foo>bar</foo>, the result will be array(bar)
            // make the result just 'bar'
            if (count($mResult) == 1 && isset($mResult[0]) && !is_array($mResult[0])) {
                $mResult = $mResult[0];
            }
        }
        // get our attributes if we have any
        $arAttributes = array();
        if ($oDomNode->hasAttributes()) {
            foreach ($oDomNode->attributes as $sAttrName=>$oAttrNode) {
                // retain namespace prefixes
                $arAttributes["@{$oAttrNode->nodeName}"] = $oAttrNode->nodeValue;
            }
        }
        // check for namespace attribute - Namespaces will not show up in the attributes list
        if ($oDomNode instanceof DOMElement && $oDomNode->getAttribute('xmlns')) {
            $arAttributes["@xmlns"] = $oDomNode->getAttribute('xmlns');
        }
        if (count($arAttributes)) {
            if (!is_array($mResult)) {
                $mResult = (trim($mResult)) ? array($mResult) : array();
            }
            $mResult = array_merge($mResult, $arAttributes);
        }
        $arResult = array($oDomNode->nodeName=>$mResult);
        return $arResult;
    }
}

$sXml = <<<XML
<nodes>
    <node>text<node>
    <node>
        <field>hello<field>
        <field>world<field>
    <node>
<nodes>
XML;

$dom = new MyDOMDocument;
$dom -> loadXml($sXml);
var_dump($dom->toArray());
?>

来源位置: http://bd.php.net/manual/en/class.domdocument.php

【问题讨论】:

    标签: domdocument php


    【解决方案1】:

    您需要关闭elements,例如&lt;/node&gt;&lt;/field&gt;

    <nodes>
        <node>text<node>
    <node>
        <field>hello<field>
        <field>world<field>
    <node>
    <nodes>
    

    改为:

    <nodes>
        <node>text</node>
    <node>
        <field>hello</field>
        <field>world</field>
    </node>
    </nodes>
    

    【讨论】:

      【解决方案2】:

      将您的 XML 更改为:

      $sXml = <<<XML
      <nodes>
          <node>text</node>
          <node>
              <field>hello</field>
              <field>world</field>
          </node>
      </nodes>
      XML;
      

      注意&lt;nodes&gt;&lt;/nodes&gt;&lt;node&gt;&lt;/node&gt;&lt;field&gt;&lt;/field&gt; 这对。您必须将每个打开的标签与它的关闭标签匹配。 See w3Schools for more about XML.

      【讨论】:

        猜你喜欢
        • 2014-05-03
        • 1970-01-01
        • 1970-01-01
        • 2016-08-27
        • 2011-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-27
        相关资源
        最近更新 更多