I am using XMLWriter to create xml.Below is my code which is working fine.
<?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:
1354510317 hello abcdefg
EDIT:
In Mozilla I am getting below erro:
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?
If you echo the time outside the XML content, then it’s no longer valid XML which is why the browser is complaining.
Specifying the content type as text/xml tells the browser to parse the XML and render it in a readable format.
If you view the source of your failing code, you should see the time followed be the raw XML content.
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.
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.