【问题标题】:SimpleXMLElement::__construct(): Entity: line 68: parser errorSimpleXMLElement::__construct():实体:第 68 行:解析器错误
【发布时间】:2016-07-06 16:18:01
【问题描述】:

我正在尝试将我的 XML 解析为 MYSQL 数据库。但我得到一个错误:

警告:SimpleXMLElement::__construct():实体:第 68 行:解析器错误:开始和结束标记不匹配:命中第 3 行和第 72 行 ******* 中的事件

我想我拼错了一些东西,但我似乎找不到它。这是我第一次使用 XML。

这是我的代码:

$xml = new SimpleXMLElement($xmlstr);
echo 'single value: <br />';
echo $xml->events->hits->hits->hits->hits[0]->_id; // get single value

这里是 $xmlstr:

$xmlstr = <<<XML
<events>
   <hits>200</hits>
   <hits>
      <_shards>
         <failed>0</failed>
         <successful>5</successful>
         <total>5</total>
      </_shards>
      <hits>
         <hits>
            <hits>
               <_id>2307</_id>
               <_index>events</_index>
               <_score null="true" />
               <_type>event</_type>
               <fields>
                  <description>
                     <hits>Bla</hits>
                  </description>
                  <genres>
                     <hits>
                        <hits>Classic</hits>
                        <hits>Rock</hits>
                        <hits>Pop</hits>
                     </hits>
                  </genres>
                  <header>
                     <hits>https://media.hugo.events/events/2307/56820e2ac5fa1eecf115fc8aaf250e9c.jpg</hits>
                  </header>
                  <logo>
                     <hits>https://media.hugo.events/events/2307/240e3f379e5996ac4c5e20b9c874cc6f.png</hits>
                  </logo>
                  <name>
                     <hits>Cultureel Festival Baarn 2016</hits>
                  </name>
                  <start>
                     <hits>2016-09-02T12:00:00+0000</hits>
                  </start>
                  <venue.city>
                     <hits>Baarn</hits>
                  </venue.city>
                  <venue.country>
                     <hits>Nederland</hits>
                  </venue.country>
                  <venue.location>
                     <hits>5.2906804</hits>
                     <hits>52.210896</hits>
                  </venue.location>
               </fields>
               <sort>
                  <hits>1472817600000</hits>
               </sort>
            </hits>
        </hits>
    </hits>
</events>
XML;

我在指向要解析的部分时做错了吗?

我希望有人可以帮助我。

提前致谢。

【问题讨论】:

  • 如错误消息所述,您的 XML 无效。 (你开始 4 个&lt;hits&gt; 标签,只关闭 3 个。)

标签: php mysql xml


【解决方案1】:

您好,我看到您的 xml 无效。我还检查了它,最后发现缺少关闭 &lt;/hits&gt;。使用的验证器:https://validator.w3.org/check 另外,在开头添加&lt;?xml version="1.0"?&gt; 也是一个不错的尝试

【讨论】:

  • 我摆脱了错误,但现在又出现了另一个错误:注意:尝试在第 64 行的 ******** 中获取非对象的属性
  • 第 64 行是最后一行
  • 嘿,你的第一个 惹麻烦了。试试这个 $xml->events->hits[1]->hits->hits->hits->_id;
  • 这应该可以工作 echo $xml->hits[1]->hits->hits->hits->_id;
猜你喜欢
  • 2016-06-23
  • 2017-02-17
  • 1970-01-01
  • 2015-09-14
  • 2021-11-28
  • 1970-01-01
  • 2021-11-28
  • 2011-04-17
  • 2011-10-27
相关资源
最近更新 更多