【问题标题】:Error in simplexml_load_string functionsimplexml_load_string 函数中的错误
【发布时间】:2013-02-14 00:37:30
【问题描述】:

我在变量 php 中有以下字符串

<root>
  <id></id>
  <title>Maarch Capture Connector</title>
  <width>400</width>
  <height>250</height>
  <center>true</center>
  <fields>
    <node>
        <type>comboBox</type>
        <id>ingoing</id>
        <label>Courrier</label>
        <autocomplete>false</autocomplete>
        <values>
            <node>
                <id>Y</id>
                <label>Arrivée</label>
            </node>
            <node>
                <id>N</id>
                <label>Départ</label>
            </node>
        </values>
        <default_value>Y</default_value>
        <show_id>false</show_id>
        <mandatory>true</mandatory>
    </node>
    <node>
        <type>comboBox</type>
        <id>priority</id>
        <label>Priorité</label>
        <autocomplete>false</autocomplete>
        <values>
            <node>
                <id>0</id>
                <label>Priorité trés haute</label>
            </node>
            <node>
                <id>1</id>
                <label>Priorité haute</label>
            </node>
            <node>
                <id>2</id>
                <label>Priorité normale</label>
            </node>
            <node>
                <id>3</id>
                <label>Priorité basse</label>
            </node>
        </values>
        <default_value>2</default_value>
        <show_id>false</show_id>
        <mandatory>true</mandatory>
    </node>
    <node>
        <type>comboBox</type>
        <id>services</id>
        <label>Service</label>
        <autocomplete>false</autocomplete>
        <values>
            <node>
                <id>9</id>
                <label>Compay SEGUNDO</label>
            </node>
            <node>
                <id>10</id>
                <label>Contract 1 for Eric SPRITZ : Life insurance</label>
            </node>
            <node>
                <id>13</id>
                <label>Contract 1 for Thomas BECK : Life insurance</label>
            </node>
            <node>
                <id>11</id>
                <label>Contract 2 for Eric SPRITZ : Home insurance</label>
            </node>
            <node>
                <id>14</id>
                <label>Contract 2 for Thomas BECK : Home insurance</label>
            </node>
            <node>
                <id>12</id>
                <label>Contract 3 for Eric SPRITZ : Car insurance</label>
            </node>
            <node>
                <id>15</id>
                <label>Contract 3 for Thomas BECK : Car insurance</label>
            </node>
            <node>
                <id>8</id>
                <label>Edith PIAF</label>
            </node>
            <node>
                <id>1</id>
                <label>Eric SPRITZ</label>
            </node>
            <node>
                <id>5</id>
                <label>Joao GILBERTO</label>
            </node>
            <node>
                <id>6</id>
                <label>Luciano PAVAROTTI</label>
            </node>
            <node>
                <id>7</id>
                <label>Maria BETHANIA</label>
            </node>
            <node>
                <id>16</id>
                <label>MARTIN HR file</label>
            </node>
            <node>
                <id>17</id>
                <label>SMITH HR file</label>
            </node>
            <node>
                <id>3</id>
                <label>Teresa CRISTINA</label>
            </node>
            <node>
                <id>2</id>
                <label>Thomas BECK</label>
            </node>
            <node>
                <id>4</id>
                <label>Tom JOBIM</label>
            </node>
        </values>
        <show_id>false</show_id>
        <mandatory>true</mandatory>
    </node>
</fields>
</root>

当我尝试使用 simplexml_load_string 函数阅读时

$xmlForm = simplexml_load_string($string);

我收到此错误

实体:第 1 行:解析器错误:需要开始标记,未找到“

我不明白什么是成功的。

【问题讨论】:

  • 尝试添加&lt;?xml version='1.0'?&gt; 并向我们展示您是如何初始化字符串的。
  • 尝试添加&lt;?xml PI 标头。在文件开头寻找不可见的字符;使用十六进制编辑器。
  • 我将它粘贴到 xmlvalidation.com 中,它现在报告错误,所以可能有一些额外的字符。

标签: php simplexml


【解决方案1】:

我已经解决了我的问题。我的 XML 字符串很好...问题出在我修改过的 Maarch Enterprise 的其他文件中。抱歉回复晚了,感谢您的帮助:)

【讨论】:

    【解决方案2】:

    执行这段代码:

    <?php
    
    $str =<<<XML_CONTENT
    <?xml version='1.0'?>
    <root>
      <id></id>
      <title>Maarch Capture Connector</title>
      <width>400</width>
      <height>250</height>
      <center>true</center>
      <fields>
        <node>
            <type>comboBox</type>
            <id>ingoing</id>
            <label>Courrier</label>
            <autocomplete>false</autocomplete>
            <values>
                <node>
                    <id>Y</id>
                    <label>Arrivée</label>
                </node>
                <node>
                    <id>N</id>
                    <label>Départ</label>
                </node>
            </values>
            <default_value>Y</default_value>
            <show_id>false</show_id>
            <mandatory>true</mandatory>
        </node>
        <node>
            <type>comboBox</type>
            <id>priority</id>
            <label>Priorité</label>
            <autocomplete>false</autocomplete>
            <values>
                <node>
                    <id>0</id>
                    <label>Priorité trés haute</label>
                </node>
                <node>
                    <id>1</id>
                    <label>Priorité haute</label>
                </node>
                <node>
                    <id>2</id>
                    <label>Priorité normale</label>
                </node>
                <node>
                    <id>3</id>
                    <label>Priorité basse</label>
                </node>
            </values>
            <default_value>2</default_value>
            <show_id>false</show_id>
            <mandatory>true</mandatory>
        </node>
        <node>
            <type>comboBox</type>
            <id>services</id>
            <label>Service</label>
            <autocomplete>false</autocomplete>
            <values>
                <node>
                    <id>9</id>
                    <label>Compay SEGUNDO</label>
                </node>
                <node>
                    <id>10</id>
                    <label>Contract 1 for Eric SPRITZ : Life insurance</label>
                </node>
                <node>
                    <id>13</id>
                    <label>Contract 1 for Thomas BECK : Life insurance</label>
                </node>
                <node>
                    <id>11</id>
                    <label>Contract 2 for Eric SPRITZ : Home insurance</label>
                </node>
                <node>
                    <id>14</id>
                    <label>Contract 2 for Thomas BECK : Home insurance</label>
                </node>
                <node>
                    <id>12</id>
                    <label>Contract 3 for Eric SPRITZ : Car insurance</label>
                </node>
                <node>
                    <id>15</id>
                    <label>Contract 3 for Thomas BECK : Car insurance</label>
                </node>
                <node>
                    <id>8</id>
                    <label>Edith PIAF</label>
                </node>
                <node>
                    <id>1</id>
                    <label>Eric SPRITZ</label>
                </node>
                <node>
                    <id>5</id>
                    <label>Joao GILBERTO</label>
                </node>
                <node>
                    <id>6</id>
                    <label>Luciano PAVAROTTI</label>
                </node>
                <node>
                    <id>7</id>
                    <label>Maria BETHANIA</label>
                </node>
                <node>
                    <id>16</id>
                    <label>MARTIN HR file</label>
                </node>
                <node>
                    <id>17</id>
                    <label>SMITH HR file</label>
                </node>
                <node>
                    <id>3</id>
                    <label>Teresa CRISTINA</label>
                </node>
                <node>
                    <id>2</id>
                    <label>Thomas BECK</label>
                </node>
                <node>
                    <id>4</id>
                    <label>Tom JOBIM</label>
                </node>
            </values>
            <show_id>false</show_id>
            <mandatory>true</mandatory>
        </node>
    </fields>
    </root>
    XML_CONTENT;
    
    $xmlForm = simplexml_load_string($str);
    
    print_r($xmlForm);
    
    echo "Everything is fine !";
    
    ?>
    

    在 PHP 在线机器中,例如:PHP online

    工作正常,所以你的字符串初始化可能有问题(坏的不可见的caraters...?)

    注意:添加 XML 标头标记是尽最大努力,不应忘记

    【讨论】:

    • 我忘了说我正在尝试安装Maarch Capture Connector
    • 与问题有什么关系..?你如何初始化旅游 XML 字符串。?
    【解决方案3】:

    尝试用&lt;id/&gt;替换空标签&lt;id&gt;&lt;/id&gt;

    虽然技术上是正确的,但它可能会导致解析器出错。

    除此之外,对我来说,它看起来像是格式良好的 XML。

    【讨论】:

    • 我不认为这是自闭标签问题。查看错误信息...
    猜你喜欢
    • 1970-01-01
    • 2011-07-22
    • 2015-08-30
    • 2011-02-23
    • 2020-02-19
    • 2019-02-25
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多