PHP and XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VarwigDude
Forum Newbie
Posts: 12
Joined: Tue Mar 31, 2009 8:08 pm

PHP and XML

Post by VarwigDude »

I'm trying to Write a XML file with PHP The Code I have is

Code: Select all

 
<?php
 
header("Content-type: text/xml");
 
$myServer = "MYSERVER\SQLEXPRESS";
$myUser = "sa";
$myPass = "MYPW";
$myDB = "cars"; 
 
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 
 
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB"); 
 
//declare the SQL statement that will query the database
$query = "SELECT id, type, year ";
$query .= "FROM cars";
 
//execute the SQL query and return records
$result = mssql_query($query); 
 
//display the results 
while($row = mssql_fetch_array($result))
{
    $yearrow = $row['year'];
    $typerow = $row['type'];
 
    echo "<note>";
        echo "<from>$yearrow</from>";
        echo "<to>$typerow</to>";
    echo "</note>";
 
}
 
//close the connection
mssql_close($dbhandle);
 
?>
 
The Data come out correctly but i get the Error "Only one top level element is allowed in an XML document. Error processing resource" and "Only one top level element is allowed in an XML document. Error processing resource"

Can anyone help!

Thanks Guys
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP and XML

Post by requinix »

http://en.wikipedia.org/wiki/XML
Read the Well-formedness section - at the very least the first paragraph.
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: PHP and XML

Post by juma929 »

Hello,

You havnt stated that its an XML document and the version. To be well formed you need to do this, to be valid you also need a DOC TYPE.

Put the following right at the beginning of your xml file:

Code: Select all

 
echo '<?xml version="1.0"?>';
 
Make sure nothing is sent before the line of code i've gave you there.

Thanks.
VarwigDude
Forum Newbie
Posts: 12
Joined: Tue Mar 31, 2009 8:08 pm

Re: PHP and XML

Post by VarwigDude »

echo '<?xml version="1.0"?>';. Worked Thank You :D
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: PHP and XML

Post by juma929 »

Hello,

No problem at all, if you need any more help with PHP/XML please let me know and ill do my best :).

Thanks,
Justin
Post Reply