473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Blank line at beginning of XML Doc

Hi.
First I apologise if there is a better place to post this. Please let
me know. I also apologise in advance if this is a really stupid
mistake, but now seem to be bashing head against wall.
I have some code that is creating an xml doc

$dom = new DOMDocument();
$xmlDoc = $dom->appendChild($dom->createElement('root'));
etc.

that is then echo d

$dom->formatOutput = true;
ob_clean();
$dom->save('php://output');

also tried
echo $dom->saveXML();

and it results in (inside the -----)
----------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<root>
<user id="9">
<firstname>Tim</firstname>
<lastname>D</lastname>
<email></email>
<houseName>The Station</houseName>

<houseNo>259</houseNo>
<street>Waterloo Rd</street>
<village>Waterloo</village>
<town>London</town>
<county>London</county>
<postcode>SE1 8RW</postcode>

</user>
</root>

-----------------------------------------------------------------------
Which looks a bit odd - in Firefox there is a blank line before the
xml
and I get the message (By the time it gets back to login.php it has
gone back through a HTTPResponse body.

Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: XML
declaration allowed only at the start of the document in Entity, line:
2 in /web/zzz/login.php on line 40
Which makes me think the newline at the beginning is causing the
problem

Any ideas ?
Thanks for any help
Aug 9 '08 #1
4 4949
dj********@googlemail.com wrote:
Hi.
First I apologise if there is a better place to post this. Please let
me know. I also apologise in advance if this is a really stupid
mistake, but now seem to be bashing head against wall.
I have some code that is creating an xml doc

$dom = new DOMDocument();
$xmlDoc = $dom->appendChild($dom->createElement('root'));
etc.

that is then echo d

$dom->formatOutput = true;
ob_clean();
$dom->save('php://output');

also tried
echo $dom->saveXML();

and it results in (inside the -----)
----------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<root>
<user id="9">
<firstname>Tim</firstname>
<lastname>D</lastname>
<email></email>
<houseName>The Station</houseName>

<houseNo>259</houseNo>
<street>Waterloo Rd</street>
<village>Waterloo</village>
<town>London</town>
<county>London</county>
<postcode>SE1 8RW</postcode>

</user>
</root>

-----------------------------------------------------------------------
Which looks a bit odd - in Firefox there is a blank line before the
xml
and I get the message (By the time it gets back to login.php it has
gone back through a HTTPResponse body.

Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: XML
declaration allowed only at the start of the document in Entity, line:
2 in /web/zzz/login.php on line 40
Which makes me think the newline at the beginning is causing the
problem

Any ideas ?
Thanks for any help
It's impossible to tell from just limited excerpts of your code that you
posted. It could be almost anything.

First thing I would suggest is look for any white space outside of the
<?php ... ?tags - this includes new lines, etc. For instance, if you
include another php file, do you have a newline after the php end tag?

Otherwise, please put together a *small* example which shows the problem
and post it here so we can look at it more closely.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 9 '08 #2
Hi - sorry its taken a while to get back. Combination of a short hol
and trying to solve it myself.
The set up I have is as follows (Its complicated due to trying to use
a sort of MVC and REST)
A Link from a homepage to login.php

Logon.php then creates a verification class that takes the email
address and checks against a db. This returns a userid that is then
used in a call to UserProfile.

In UserProfile I am trying to use REST so I have the following code

public function exec($u) {

//print 'In user profile';
$req =& new HTTP_Request("http://localhost/sbc/dbCaller/user/$u");
$req->addHeader('HTTP_ACCEPT', 'application/xml,application/xhtml
+xml,application/xml;q=0.9,*/*;q=0.8');
if (!PEAR::isError($req->sendRequest())) {
req->getResponseBody();

Using apache readback this gets caught by dbCaller
dbCaller is just a handler that at the moment just creates a test
class (see below)
Test uses the rest of the url to work out what it needs to return and
creates a DOM object of the xml.
The creation seems to work ok, no exceptions. and the return to
dbCaller results in a DOMDocument object in dbCaller.
But it then falls down . I dont seem to be able to get the
DOMDocument object back through the http response. (In UserProfile)
Have tried creating a DOMDocument object and loading the responsebody
into it - doesnt work. In fact I dont seem to be able to send a
DOMDocument back through the response, and
am begining to think its not possible. Am new to this and may be
missing something v obvious.

Any comments of any sort welcome :-)

test class

<?php
/*
* Created on 4 Aug 2008
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
require_once ('Database.php'); // Standard functions.

class test {
function exec() {
$conn = new dbConnection;
$c = $conn->getConn();
//print 'In test';

$urlString = substr($_SERVER['REQUEST_URI'], strlen('/sbc/
dbCaller'));

$urlParts = explode('/', $urlString); //create an array of the parts
after the base url
$lastPart = array_pop($urlParts); //get the last one

$dotPosition = strpos($lastPart, '.'); // figure out what
extension is required
if ($dotPosition !== FALSE) {
$this->extension = substr($lastPart, $dotPosition + 1);
$lastPart = substr($lastPart, 0, $dotPosition);
}
array_push($urlParts, $lastPart);

if (isset($urlParts[0]) && $urlParts[0] == '') {
array_shift($urlParts);
}

if (isset($urlParts[0])) $this->table = $urlParts[0];
$sql = 'SELECT * FROM '. $this->table;
$row = $c->query($sql);
$res = $row->fetch(PDO::FETCH_ASSOC);
$arr = array_keys($res);
$cols = $row->columnCount();

if (count($urlParts) 1 && $urlParts[1] != '') {
array_shift($urlParts);
$arrCount = count($urlParts);
foreach ($urlParts as $uid) {
if ($uid != '') {
//print $uid;
$this->uid[] = $uid;
}
}
}
//build the query
$sql2 = 'SELECT * FROM '. $this->table;
//print 'Arr' . $arrCount;
if ($arrCount >= 1) {
$sql2 .= ' WHERE ' . $arr[0] . ' = ' . $this->uid[0];
if ($arrCount >= 2) {
for ($f=1; $f<=($arrCount-1); $f++) {
$sql2 .= ' AND ' . $arr[$f] . ' = ' . $this->uid[$f];
}
}
}
//print 'SQL' . $sql2;
$dom = new DOMDocument();
$xmlDoc = $dom->appendChild($dom->createElement('root'));
foreach ($c->query($sql2) as $row2) {
$rec = $xmlDoc->appendChild($dom->createElement($this->table));
$rec->setAttribute($arr[0], $row2[$arr[0]]);
for ($i= 1; $i< $cols; $i++) {
if ($arr[$i] != 'password') {
$rec->appendChild($dom->createElement($arr[$i], $row2[$arr[$i]]));
}
}
}
$dom->formatOutput = true;
//ob_clean();
//$dom->save('php://output');
//$p = $dom->saveXML();
return $dom;
//print $p;

}
}
?>
Aug 17 '08 #3
dj********@googlemail.com wrote:
Hi - sorry its taken a while to get back. Combination of a short hol
and trying to solve it myself.
The set up I have is as follows (Its complicated due to trying to use
a sort of MVC and REST)
A Link from a homepage to login.php

Logon.php then creates a verification class that takes the email
address and checks against a db. This returns a userid that is then
used in a call to UserProfile.

In UserProfile I am trying to use REST so I have the following code

public function exec($u) {

//print 'In user profile';
$req =& new HTTP_Request("http://localhost/sbc/dbCaller/user/$u");
$req->addHeader('HTTP_ACCEPT', 'application/xml,application/xhtml
+xml,application/xml;q=0.9,*/*;q=0.8');
if (!PEAR::isError($req->sendRequest())) {
req->getResponseBody();

Using apache readback this gets caught by dbCaller
dbCaller is just a handler that at the moment just creates a test
class (see below)
Test uses the rest of the url to work out what it needs to return and
creates a DOM object of the xml.
The creation seems to work ok, no exceptions. and the return to
dbCaller results in a DOMDocument object in dbCaller.
But it then falls down . I dont seem to be able to get the
DOMDocument object back through the http response. (In UserProfile)
Have tried creating a DOMDocument object and loading the responsebody
into it - doesnt work. In fact I dont seem to be able to send a
DOMDocument back through the response, and
am begining to think its not possible. Am new to this and may be
missing something v obvious.

Any comments of any sort welcome :-)

test class

<?php
/*
* Created on 4 Aug 2008
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
require_once ('Database.php'); // Standard functions.

class test {
function exec() {
$conn = new dbConnection;
$c = $conn->getConn();
//print 'In test';

$urlString = substr($_SERVER['REQUEST_URI'], strlen('/sbc/
dbCaller'));

$urlParts = explode('/', $urlString); //create an array of the parts
after the base url
$lastPart = array_pop($urlParts); //get the last one

$dotPosition = strpos($lastPart, '.'); // figure out what
extension is required
if ($dotPosition !== FALSE) {
$this->extension = substr($lastPart, $dotPosition + 1);
$lastPart = substr($lastPart, 0, $dotPosition);
}
array_push($urlParts, $lastPart);

if (isset($urlParts[0]) && $urlParts[0] == '') {
array_shift($urlParts);
}

if (isset($urlParts[0])) $this->table = $urlParts[0];
$sql = 'SELECT * FROM '. $this->table;
$row = $c->query($sql);
$res = $row->fetch(PDO::FETCH_ASSOC);
$arr = array_keys($res);
$cols = $row->columnCount();

if (count($urlParts) 1 && $urlParts[1] != '') {
array_shift($urlParts);
$arrCount = count($urlParts);
foreach ($urlParts as $uid) {
if ($uid != '') {
//print $uid;
$this->uid[] = $uid;
}
}
}
//build the query
$sql2 = 'SELECT * FROM '. $this->table;
//print 'Arr' . $arrCount;
if ($arrCount >= 1) {
$sql2 .= ' WHERE ' . $arr[0] . ' = ' . $this->uid[0];
if ($arrCount >= 2) {
for ($f=1; $f<=($arrCount-1); $f++) {
$sql2 .= ' AND ' . $arr[$f] . ' = ' . $this->uid[$f];
}
}
}
//print 'SQL' . $sql2;
$dom = new DOMDocument();
$xmlDoc = $dom->appendChild($dom->createElement('root'));
foreach ($c->query($sql2) as $row2) {
$rec = $xmlDoc->appendChild($dom->createElement($this->table));
$rec->setAttribute($arr[0], $row2[$arr[0]]);
for ($i= 1; $i< $cols; $i++) {
if ($arr[$i] != 'password') {
$rec->appendChild($dom->createElement($arr[$i], $row2[$arr[$i]]));
}
}
}
$dom->formatOutput = true;
//ob_clean();
//$dom->save('php://output');
//$p = $dom->saveXML();
return $dom;
//print $p;

}
}
?>
Well, you showed your class Test, which you indicate is working
properly. But you never showed how you're calling Test->exec() and
trying to return the document back to the caller.

But I'm wondering why you're using an HTTP_Request object here to fetch
the document, when it's on your own server. You're just creating an
additional load on the server. It would be more efficient to just
create the object of Test and create the dom document right there. As
it is, you have a huge security hole - what happens, for instance, if
someone enters on their browser
"http://www.example.com//sbc/dbCaller/user/joeblow"?

But if you insist on doing it this way, you have to handle it just like
you would a browser request. You need to echo (or print) the document.
But you can't do that directly, i.e. you can't just say echo $dom;
because it's not a simple object. You need to either serialize it (not
recommended) or convert to xml and print the resulting xml. Then you
can get the xml and convert it back to a DomDocument.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 19 '08 #4
Thanks a lot for replying Jerry. You are right on all counts.
The idea was to use REST between MVC layers as well as externaly. Have
trashed that idea for the reasons you mentioned, and I hope a better
understanding of REST. You were also right about the security hole.
Though I was aware of that and was planning to pass the headers from
the originating request through and then have the lower classes verify
by the digest in the same way.
So thanks for that and thanks for bearing with me as I worked it out.
I solved the problem by using dom->saveHTML() instead of dom-
>saveXML() and then printing that.
thanks again
Aug 25 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: dan glenn | last post by:
Hi. I'm finding that if I have text entered into a <textarea ...> </textarea> in a form, and that text has leading blank lines (with no spaces or anything else), that when I retrieve the entered...
8
by: m. verkerk | last post by:
Hi everybody! Hope someone can help me out with this! I'm sending a file to a user with the following code: header( "Content-Type: application/binary"); header( "Content-disposition:...
8
by: dmcconkey | last post by:
Hi folks, I have a client with four websites. Each site has a contact form that is identical. They all have "required" fields validated through a JavaScript onSubmit() function. Upon validation,...
5
by: ChadDiesel | last post by:
My basic question is why does my print report button on my subform print a blank report when my cursor is on a blank entry line? Here is a more detailed explanation of my problem. I have a...
3
by: BradC | last post by:
I have a big VS 2002/2003 annoyance, and wondering if there is a way to turn it off: Put your cursor at the start of an indented line of code inside a function. (right before the first...
6
by: EricR | last post by:
I am trying to bcp import a text file into a SQL Server 2000 database. The text file is coming out of a java application where order information is written to the text file. Each record is on it's...
12
by: Seller | last post by:
I am using Wordpress 2.0.6 on Fedora Core 5. An RSS feed is not working for http://survivalofthesickestthebook.com/blog/?feed=rss2 When I use feed validator...
7
by: Joy M | last post by:
Hello, I am modifying an .asp file and I noticed that the top line on the screen is blank. I would like to remove this line, and push everything up to the top of the screen, but I don't know...
4
by: Patrick Nolan | last post by:
I am using javascript to manipulate optgroups in select elements. When the results are displayed in Firefox 2.0 there is an annoying blank line at the top of the multi-line select box. This...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.