首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DOMDocument::loadXML()错误

DOMDocument::loadXML()错误
EN

Stack Overflow用户
提问于 2011-04-12 03:49:29
回答 2查看 8.9K关注 0票数 1

当我在PHP中使用DOMDocument时,我会得到以下错误:

警告: DOMDocument::loadXML() domdocument.loadxml:实体中的第7行和第77行上的第7行:\wamp\www\dom1.php中的数据过早结束 警告: DOMDocument::loadXML() domdocument.loadxml:实体中标记节点第6行中的数据过早结束,第77行中为7行D:\wamp\www\dom1.php 警告: DOMDocument::loadXML() domdocument.loadxml:实体中标记字段中数据的过早结束,实体中的第5行,D中的第7行:\wamp\www\dom1.php,第77行 警告: DOMDocument::loadXML() domdocument.loadxml:实体中标记字段中数据的过早结束,实体中的第5行,D中的第7行:\wamp\www\dom1.php,第77行 警告: DOMDocument::loadXML() domdocument.loadxml:实体中标记字段中数据的过早结束,实体中的第4行,D中的第7行:\wamp\www\dom1.php,第77行 警告: DOMDocument::loadXML() domdocument.loadxml:实体中标记字段中数据的过早结束,实体中的第4行,D中的第7行:\wamp\www\dom1.php,第77行 警告: DOMDocument::loadXML() domdocument.loadxml:实体中标记节点第3行中的数据过早结束,第77行中为7行D:\wamp\www\dom1.php 警告: DOMDocument::loadXML() domdocument.loadxml:实体中标记节点第2行中的数据过早结束,第77行中为7行D:\wamp\www\dom1.php 警告: DOMDocument::loadXML() domdocument.loadxml:实体中标记节点第2行中的数据过早结束,第77行中为7行D:\wamp\www\dom1.php 警告: DOMDocument::loadXML() domdocument.loadxml:实体中的标记节点第1行,实体中的第7行:第77行数组(0)上的wamp\www\dom1.php

php代码如下所示:

代码语言:javascript
复制
<?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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-12 03:56:49

您需要关闭elements,例如</node></field>

代码语言:javascript
复制
<nodes>
    <node>text<node>
<node>
    <field>hello<field>
    <field>world<field>
<node>
<nodes>

改为:

代码语言:javascript
复制
<nodes>
    <node>text</node>
<node>
    <field>hello</field>
    <field>world</field>
</node>
</nodes>
票数 3
EN

Stack Overflow用户

发布于 2011-04-12 04:00:31

将XML更改为:

代码语言:javascript
复制
$sXml = <<<XML
<nodes>
    <node>text</node>
    <node>
        <field>hello</field>
        <field>world</field>
    </node>
</nodes>
XML;

注意这对<nodes></nodes><node></node><field></field>。你必须把每个打开的标签和它的封闭标签匹配起来。有关XML的更多信息,请参见w3Schools。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5630194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档