我正在使用 XMLWriter 创建 xml。下面是我的代码,它工作正常。
<?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;
?>
如果使用下面的代码,我会收到错误“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;
?>
错误:对于下面的代码块是错误:我在谷歌浏览器中得到这个:
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.
如果我没有使用 header("Content-Type: text/xml;"); 然后上面的代码块正在工作,但时间正在显示并且没有给出 xml 格式。以下是错误:
1354510317 hello abcdefg
编辑: 在Mozilla我得到以下错误:
XML Parsing Error: not well-formed
Location: http://localhost/example.php
Line Number 1, Column 11:1354512268<?xml version="1.0"?>
----------^
如果我的代码做错了什么,请提出建议。
“下面是直到第一个错误的页面呈现。” 错误可以消除吗?