php 出现错误“下面是页面渲染到第一个错误为止。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13677369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:53:04  来源:igfitidea点击:

Getting Error "Below is a rendering of the page up to the first error."

php

提问by Jarod Elliott

I am using XMLWriter to create xml.Below is my code which is working fine.

我正在使用 XMLWriter 创建 xml.Below 是我的工作正常的代码。

<?php
header("Content-Type: text/xml;");
$writer = new XMLWriter(); 
$writer->openMemory();  
$writer->startDocument('1.0'); 
$writer->setIndent(4); 
$writer->startElement('epp'); 
$writer->startElement("command"); 
$writer->startElement("login"); 
$writer->writeElement('clID', 'hello'); //username
$writer->writeElement('pw', 'abcdefg'); //password
$writer->endElement(); //login
$writer->endElement(); //command
$writer->endElement(); //login
$writer->endDocument(); 
$file =  $writer->outputMemory(true); 
echo $file;

?>

If am using below code I am getting error "Getting Error "Below is a rendering of the page up to the first error.".

如果我使用下面的代码,我会收到错误“获取错误”下面是页面的呈现,直到第一个错误。”。

<?php
$time  = time();
echo $time;
header("Content-Type: text/xml;");
$writer = new XMLWriter(); 
$writer->openMemory();  
$writer->startDocument('1.0'); 
$writer->setIndent(4); 
$writer->startElement('epp'); 
$writer->startElement("command"); 
$writer->startElement("login"); 
$writer->writeElement('clID', 'hello'); //username
$writer->writeElement('pw', 'abcdefg'); //password
$writer->endElement(); //login
$writer->endElement(); //command
$writer->endElement(); //login
$writer->endDocument(); 
$file =  $writer->outputMemory(true); 
echo $file;

?>

Error:For above block of code below is the error:I am getting this in google chrome:

错误:对于上面的代码块,下面是错误:我在谷歌浏览器中得到了这个:

 This page contains the following errors:

 error on line 1 at column 1: Document is empty
 Below is a rendering of the page up to the first error.

If I am not using header("Content-Type: text/xml;"); then above block of code is working but time is displaying and not giving the xml format.Below is the error:

如果我不使用 header("Content-Type: text/xml;"); 然后上面的代码块正在工作,但时间正在显示并且没有给出 xml 格式。下面是错误:

1354510317 hello abcdefg

EDIT:In Mozilla I am getting below erro:

编辑:在 Mozilla 中,我低于错误:

XML Parsing Error: not well-formed
Location: http://localhost/example.php
Line Number 1, Column 11:1354512268<?xml version="1.0"?>
 ----------^

If I have done anything wrong with my code please suggest.

如果我的代码有任何问题,请提出建议。

How "Below is a rendering of the page up to the first error." error can be removed?

如何“下面是页面渲染,直到第一个错误。” 错误可以删除吗?

回答by Jarod Elliott

If you echo the time outside the XML content, then it's no longer valid XML which is why the browser is complaining.

如果您在 XML 内容之外回显时间,则它不再是有效的 XML,这就是浏览器抱怨的原因。

Specifying the content type as text/xml tells the browser to parse the XML and render it in a readable format.

将内容类型指定为 text/xml 会告诉浏览器解析 XML 并以可读格式呈现它。

If you view the source of your failing code, you should see the time followed be the raw XML content.

如果您查看失败代码的来源,您应该看到后面的时间是原始 XML 内容。

The reason it doesn't look right when you don't send the text/xml content type is that the browser is essentially seeing the XML tags as HTML and deciding they do nothing to alter the layout so it just shows the plain text values from within the tags.

当您不发送 text/xml 内容类型时它看起来不正确的原因是浏览器本质上将 XML 标记视为 HTML 并决定它们不做任何更改布局因此它只显示来自的纯文本值标签内。

If you want to mix normal output with XML output in your page, you would need to format the XML nicely and HTML encode it.

如果您想在页面中混合正常输出和 XML 输出,您需要很好地格式化 XML 并对其进行 HTML 编码。

回答by bala

header("Content-Type: text/xml;");

What type of header you are using there so that output should be in that format.

您在那里使用什么类型的标题,以便输出应采用该格式。

Example :

例子 :

header("Content-Type: text/xml;");

output : coutput in xml format

header("Content-Type: text/php;");

output : output in php format

回答by dasfdsa

A lot of times this error comes for HTML elements which don't require a closing tag.

很多时候这个错误来自不需要结束标记的 HTML 元素。

Bad:  <img src="https://google.com">
Good: <img src="https://google.com"/>