Click to See Complete Forum and Search --> : Write XML File using PHP/MySQL


runspace
01-13-2007, 09:39 AM
Hi,

I've written an xml file to write data to a classified listing I've set up on my site that will be queried daily by another site to update and retrieve new data and am having trouble in the following areas.

1. Looping new listings
2. Getting proper image corresponding to the current listing from another table in the db.
3. Selecting between current and not current listings.
4. After current listings are selected sorting between type of listing.
5. Error when generating xml code stating:
XML Parsing Error: xml declaration not at start of external entity
Location: http://www.fooBar.com
Line Number 3, Column 1:<?xml version="1.0" encoding="utf-8"?>
^

The logic for points 3 and 4 has not yet been coded as I'm not sure how and where to place it.

All variables are saved in a separate file and necessary files such as database connections are included

Here is my code:

<?php
header('Content-type: text/xml');

$_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

//db connections
$conn = DBConnect();
$sql = "SELECT * FROM props WHERE rec_id != ''";
$result1 = mysql_query($sql);
@$row = mysql_fetch_array($result1);
$sql2 = "SELECT file FROM pics WHERE rec_id = '$id'";
$result2 = mysql_query($sql2);
@$row2 = mysql_fetch_array($result2);

// php loop to get information from properties table and parse into the feed
for ($i = 0; $i < mysql_num_rows($result1); $i++) {

$_xml .= "<property>\n";
$_xml .= "<location>\n";
$_xml .= "<street-address>$propAddy</street-address>\n";
$_xml .= "<city-name>$propCity</city-name>\n";
$_xml .= "<county></county>\n";
$_xml .= "<state-code>$propSt</state-code>\n";
$_xml .= "<zipcode>$propZip</zipcode>\n";
$_xml .= "<display-address>Yes</display-address>\n";
$_xml .= "<location></location>\n";
$_xml .= "<details></details>\n";
$_xml .= "<price>$propPrice</price>\n";
$_xml .= "<year-built>$propYear</year-built>\n";
$_xml .= "<num-bedrooms>$propBed</num-bedrooms>\n";
$_xml .= "<num-bathrooms>$propBath</num-bathroos>\n";
$_xml .= "<apartment-number></apartment-number>\n";
$_xml .= "<lot-size></lot-size>\n";
$_xml .= "<square-feet></square-feet>\n";
$_xml .= "<date-listed></date-listed>\n";
$_xml .= "<date-sold></date-sold>\n";
$_xml .= "<description>$propDescription</description>\n";
$_xml .= "<mlsId></mlsId>\n";
$_xml .= "<details></details>\n";
$_xml .= "<agent></agent>\n";
$_xml .= "<broker-name></broker-name>\n";
$_xml .= "<agent-name></agent-name>\n";
$_xml .= "<phone-number></phone-number>\n";
$_xml .= "<email></email>\n";
$_xml .= "</agent>\n";
$_xml .= "<status>$propCategory</status>\n";
$_xml .= "<property-type></property-type>\n";
$_xml .= "<site>\n";
$_xml .= "<site-url>http://www.foobar.com</site-url>\n";
$_xml .= "<site-name>FooBar</site-name>\n";
$_xml .= "</site>\n";
$_xml .= "<pictures>\n";
$_xml .= "<picture>\n";
$_xml .= "<picture-url>$propImg</picture-url>\n";
$_xml .= "</picture>\n";
$_xml .= "</pictures>\n";
$_xml .= "<landing-page>\n";
$_xml .= "<lp-url>$propertyLink</lp-url>\n";
$_xml .= "<has-virtual-tour></has-virtual-tour>\n";
$_xml .= "</landing-page>\n";
$_xml .= "</property>\n";
}

$_xml .= "</properties>";

print $_xml;

runspace
01-13-2007, 05:21 PM
Any Suggestions?

NogDog
01-13-2007, 08:21 PM
If you do a "view source" from your browser on the result page, is there something being output prior to the <?xml tag (maybe a PHP error message)?

runspace
01-13-2007, 09:57 PM
Yes see point 5. Let me know if you have any other questions and thanks for taking a look!

NogDog
01-14-2007, 01:53 AM
I thought that was an XML parser error at the client (browser) end upon receiving the output from your script. If so, then I was interested in seeing the raw source code at the browser via a right-click->view source, just as you would with any web page to see the underlying HTML source code.

Alternatively, you could change your header() command at the start to...

header("Content-Type: text/plain")
...to assist in debugging, allowing us to see exactly what is being output.

runspace
01-14-2007, 10:11 AM
Yes you may still view the source to see what is displayed. I will get something up so that you can check it out.

Ultimater
01-14-2007, 12:29 PM
Opening tag <properties>