php“ XML解析错误:XML或文本声明不在实体开始时”

发布于 2025-01-29 17:00:31 字数 1192 浏览 0 评论 0原文

我的API处理器以“在请求中的“ type”关键字”指定的几种格式之一返回数据。在不产生错误的情况下调用此问题

<?PHP
if($_REQUEST['type'] == "XML")
{
header ("Content-Type:text/xml");
}

? 稍后,我正在使用PHP的DOM类来制定XML。 这看起来像是这种

$dom = new DOMDocument("1.0", 'utf-8');
$root = $dom->createElement("Data");
$dom->appendChild($root);
if(!empty($Error))
{
    $Er = $dom->createElement("Errors");
    $root->appendChild($Er);
    foreach($Error as $value)
    {
        $key = "Error";
        $Child = $dom->createElement($key);
        $Child = $Er->appendChild($Child);
        $data = $dom->createTextNode($value);
        $data = $Child->appendChild($data);
    }
}
else
{
    foreach($XMLItems as $key => $value)
    {
        $key = $dom->createElement($key);
        $root->appendChild($key);
        $variable = $dom->createTextNode($value);
        $key->appendChild($variable);
    }
}
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = TRUE;
echo $dom->saveXML();

解决方案:根据Aynber的建议,我在这里解决了问题,就是消除PHP中的任何空白行,以及任何包含的任何空白。我取消了关闭PHP标签和额外的线路,其中包括和主文件。这消除了文件顶部的两个空白线,使我可以插入XML标头。是否需要消除关闭标签,这可能是值得怀疑的,但它们不需要在那里。

My API processor returns data in one of several formats designated by the "type' keyword in the request. I am able to invoke, for instance, a JSON header using the following method, but this does not work for XML. Is there a way of invoking this without producing an error?

<?PHP
if($_REQUEST['type'] == "XML")
{
header ("Content-Type:text/xml");
}

There is no white space in the header designation.
Later down the line, I am using PHP's dom class to formulate the XML.
This looks like this

$dom = new DOMDocument("1.0", 'utf-8');
$root = $dom->createElement("Data");
$dom->appendChild($root);
if(!empty($Error))
{
    $Er = $dom->createElement("Errors");
    $root->appendChild($Er);
    foreach($Error as $value)
    {
        $key = "Error";
        $Child = $dom->createElement($key);
        $Child = $Er->appendChild($Child);
        $data = $dom->createTextNode($value);
        $data = $Child->appendChild($data);
    }
}
else
{
    foreach($XMLItems as $key => $value)
    {
        $key = $dom->createElement($key);
        $root->appendChild($key);
        $variable = $dom->createTextNode($value);
        $key->appendChild($variable);
    }
}
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = TRUE;
echo $dom->saveXML();

Solution: What I did to solve the problem here, following aynber's suggestions, is to eliminate any blank lines in the PHP as well as any includes. I eliminated closing PHP tags and extra lines in those includes as well as the main file. This eliminated the two blank lines at the top of the file, allowing me to insert the XML header. Whether eliminating the closing tags was necessary may be questionable, but they do not need to be there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2025-02-05 17:00:31

“ XML解析错误:XML或文本声明不是在实体开始时”表示在XML输出开始时的某个地方,有一个不应该在那里的空间或其他字符。有几个地方可以检查:

  • 每个PHP文件的开头。确保在&lt;?php之前没有空格,新行或隐形字符>
  • 您脱离并返回到PHP块中的任何地方。即使是空白空间,那里的任何东西都会发送到浏览器。

"XML Parsing Error: XML or text declaration not at start of entity" means that somewhere at the start of your XML output, there is a space or other character that's not supposed to be there. There are a few places to check:

  • The beginning of every PHP file. Make sure there are no spaces, new lines, or invisible characters before <?php
  • Any place you break out of and back into the PHP blocks. Anything there will be sent to the browser, even if it is white space.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文