【问题标题】:php simplexml_load_string not working on XML stringphp simplexml_load_string 不适用于 XML 字符串
【发布时间】:2019-11-12 10:26:14
【问题描述】:

我正在尝试使用 PHP 函数 simplexml_load_string 读取 XML 字符串,但出现以下错误

Message: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found

Message: simplexml_load_string(): &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

Message: simplexml_load_string(): ^

我在如下变量 (var_dum) 中得到 XML 字符串。我使用 fgetc 从另一个文件中获取了这个 XML 字符串,最后我也尝试了 '/xml' 但它给出了同样的错误

     string(3155) "<?xml version="1.0" encoding="UTF-8"?>
    <LEIRecord xmlns:lei="http://www.gleif.org/data/schema/leidata/2016">
          <LEI>029200013A5N6ZD0F605</LEI>
          <Entity>
            <LegalName xml:lang="en">AFRINVEST SECURITIES LIMITED</LegalName>
            <OtherEntityNames>
              <OtherEntityName xml:lang="fr" type="ALTERNATIVE_LANGUAGE_LEGAL_NAME">AFRINVEST SECURITIES LIMITED</OtherEntityName>
            </OtherEntityNames>
            <LegalAddress xml:lang="en">
              <FirstAddressLine>27 GERRARD ROAD</FirstAddressLine>
              <AdditionalAddressLine>IKOYI</AdditionalAddressLine>
              <City>LAGOS</City>
              <Region>NG-LA</Region>
              <Country>NG</Country>
              <PostalCode>23401</PostalCode>
            </LegalAddress>
            <LegalJurisdiction>NG</LegalJurisdiction>
            <LegalForm>
              <EntityLegalFormCode>8888</EntityLegalFormCode>
              <OtherLegalForm>LIMITED</OtherLegalForm>
            </LegalForm>
            <EntityStatus>ACTIVE</EntityStatus>
          </Entity>
        </LEIRecord>
    "

代码:

json_decode(json_encode((array)simplexml_load_string($data)), TRUE);

【问题讨论】:

  • 显示你的simplexml_load_string 函数调用代码
  • 有问题更新
  • $data 是从HTML chars 转义的...

标签: php xml


【解决方案1】:

似乎$data 是从HTML chars 中转义的...

在传递给simplexml_load_string..之前尝试解码。

代替

json_decode(json_encode((array)simplexml_load_string($data)), TRUE);

试试这个...

json_decode(json_encode((array)simplexml_load_string(htmlspecialchars_decode($data))), TRUE);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-14
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 2012-09-11
    相关资源
    最近更新 更多