| | |
I'm way out of my depth and need desperate help.
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
All I am trying to do is create an XML feed so that I can submit it to a site. However no matter what I do (albeit with my limited size of brain) all I get are errors. It hates pound signs and apostrophes even if I try the only thing I know which is string replaces. Please someone help. I will post this in the XML section too.
php Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>RAD Feed</title> </head> <body> <?php $username = ""; //Database Username $password = ""; //Database Password $hostname = ""; //Database Host $dbname = ""; //Database (or Catalog in MySQL parlance) name $dbh = mysql_connect($hostname, $username, $password) or die("Could not connect to database server"); $selected = mysql_select_db($dbname, $dbh) or die("Database not found or problem connecting"); $result = mysql_query("SELECT position, postdate, jobref, jobid, country, description FROM jobs"); // if the file exists already, delete it first to flush data $xmlfeedfile = "jobfeed.xml"; $filehandle = fopen($xmlfeedfile, 'w'); $itemLink = $fullurl.'/info_jobid_'.$b[jobid].'.html'; $xmlString = '<?xml version="1.0" encoding="utf-8"?> <source> <publisher></publisher> <publisherurl></publisherurl>'; fwrite($filehandle, $xmlString); while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $xmlString = '<job> <title>' . $row{position} . '</title> <date>' . $row{postdate} . '</date> <referencenumber>' . $row{jobref} . '</referencenumber> <url><a href="http://www.*****.co.uk' . $fullurl . '/info_jobid_' . $row{jobid} . '.html" target="job' . $row{jobid} . '">' . $row{jobref} . '</a></url> <country>United Kingdom</country> <description>' . $row{description} . '</description> </job> '; fwrite($filehandle, $xmlString); } mysql_close($dbh); fwrite($filehandle, "</source>"); fclose($filehandle); ?> </body> </html>
Last edited by peter_budo; Jul 20th, 2009 at 12:17 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Split up the xml open and close tags.
PHP Syntax (Toggle Plain Text)
$string = "<" . "?" . "........" . "?" . ">";
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
No, your problem seems to be that the php page is trying to make sense of the <?xml .....?> tags. So change:
to
I may have overkilled the concatenation, but give it a go.
PHP Syntax (Toggle Plain Text)
$xmlString = '<?xml version="1.0" encoding="utf-8"?> <source> <publisher></publisher> <publisherurl></publisherurl>';
PHP Syntax (Toggle Plain Text)
$xmlString = '< ' . '?' . 'xml version="1.0" encoding="utf-8"' . '?' .'> <source> <publisher></publisher> <publisherurl></publisherurl>';
I may have overkilled the concatenation, but give it a go.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
I'd do something like this - notice it's good practice to 'clean' the output. Also If you've got html markup in the db, and you want clean text output, you could use the strip_tags() function.
\n = create a new line
\t = indent the tag (same as a TAB)
This just beautifies the xml tags
IMPT - notice that I changed the single quotes for double quotes. You can't put 'braced' variables, like {$fullurl} inside single quotes nor will \n and \t work inside them.
My method, although not everybody's cup of tea, helps me avoid losing my head up my own derriere when it comes to millions of quotes and concatenators (dots).
Your original problem with '<div>' suggests that the description field in the database contains html tags. These can sometimes mess with output. As I mentioned earlier, you could get rid of them completely with strip_tags() or convert them to visible tags by using htmlentities($row['description']).
Either of these should allow you to get some tidy output to the XML file.
PHP Syntax (Toggle Plain Text)
$pos = stripslashes($row['position']); $date = stripslashes($row['postdate']); $ref = stripslashes($row['jobref']); $desc = stripslashes($row['jobref']); $jid = stripslashes($row['jobid']); //OR $desc = stripslashes(strip_tags($row['desc'])); ETC, ETC $xmlString = "<job>\n\t<title>{$pos}</title>\n\t<date>{$date}</date>\n\t <referencenumber>{$jobref}</referencenumber>\n\t<url><a href=\"http://www.*****.co.uk{$fullurl}/info_jobid_{$jobid}.html\" target=\"job{$jobid}\">{$jobref}</a></url>\n\t<country>United Kingdom</country>\n\t<description>{$desc}</description>\n</job>";
\n = create a new line
\t = indent the tag (same as a TAB)
This just beautifies the xml tags
IMPT - notice that I changed the single quotes for double quotes. You can't put 'braced' variables, like {$fullurl} inside single quotes nor will \n and \t work inside them.
My method, although not everybody's cup of tea, helps me avoid losing my head up my own derriere when it comes to millions of quotes and concatenators (dots).
Your original problem with '<div>' suggests that the description field in the database contains html tags. These can sometimes mess with output. As I mentioned earlier, you could get rid of them completely with strip_tags() or convert them to visible tags by using htmlentities($row['description']).
Either of these should allow you to get some tidy output to the XML file.
Last edited by ardav; Jul 20th, 2009 at 1:35 pm. Reason: typos
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
•
•
Join Date: Jul 2008
Posts: 149
Reputation:
Solved Threads: 25
Why even bother with all of the sanitizing when the php5 DOM does this for you.
Check out the following code...it is based on what you originally posted and creates the same XML. Its not a perfect representation of what your script does but i wanted to make sure it was something you could run and see the output of.
It will look a bit overwhelming, but its very repetitive. This is something that would best be broken into a function that is called in the loop but actually seeing the DOM should help you understand what is going on.
Notice how it encodes the url html code. For it to be valid xml the a tag would either be part of the xml or needs to be encoded into its entities which is what the DOM does by default or it could be wrapped in a CDATA tag.
Check out of the DOM info on php.net
Produces
Check out the following code...it is based on what you originally posted and creates the same XML. Its not a perfect representation of what your script does but i wanted to make sure it was something you could run and see the output of.
It will look a bit overwhelming, but its very repetitive. This is something that would best be broken into a function that is called in the loop but actually seeing the DOM should help you understand what is going on.
Notice how it encodes the url html code. For it to be valid xml the a tag would either be part of the xml or needs to be encoded into its entities which is what the DOM does by default or it could be wrapped in a CDATA tag.
Check out of the DOM info on php.net
php Syntax (Toggle Plain Text)
<?php $result = array(); $result[] = array( 'position' => 'some job', 'postdate' => '01/01/2009', 'jobref' => 'abc12345', 'jobid' => '0987654321', 'description' => 'this is a description' ); $result[] = array( 'position' => 'some other job', 'postdate' => '01/02/2009', 'jobref' => 'abc1212341234345', 'jobid' => '09876123454321', 'description' => 'this is another description' ); $result[] = array( 'position' => 'some asdfasdfjob', 'postdate' => '01/01/2037', 'jobref' => 'abc1234sdfgsdf5', 'jobid' => 'sdfg0987654321', 'description' => 'this is the last description' ); $fullurl = ''; $doc = new DOMDocument('1.0', 'utf-8'); $doc->formatOutput = true; //Create the source node $src = $doc->createElement( 'source' ); $doc->appendChild( $src ); //create the source/publisher node $pub = $doc->createElement( 'publisher' ); $src->appendChild( $pub ); //create the source/publisherurl node $pubUrl = $doc->createElement( 'publisherurl' ); $src->appendChild( $pubUrl ); //Loop over sql results and create source/job nodes foreach( $result as $row ) { //Create /source/job node $job = $doc->createElement( 'job' ); //Create source/job[]/title node $title = $doc->createElement( 'title' ); $title->appendChild( $doc->createTextNode( $row['position'] ) ); $job->appendChild( $title ); //Create source/job[]/date node $date = $doc->createElement( 'date' ); $date->appendChild( $doc->createTextNode( $row['postdate'] ) ); $job->appendChild( $date ); //Create source/job[]/referencenumber node $ref = $doc->createElement( 'referencenumber' ); $ref->appendChild( $doc->createTextNode( $row['jobref'] ) ); $job->appendChild( $ref ); //Create source/job[]/url node $url = $doc->createElement( 'url' ); $url->appendChild( $doc->createTextNode( '<a href="http://www.*****.co.uk' . $fullurl . '/info_jobid_' . $row['jobid'] . '.html" target="job' . $row['jobid'] . '">' . $row['jobref'] . '</a>' ) ); $job->appendChild( $url ); //Create source/job[]/country node $country = $doc->createElement( 'country' ); $country->appendChild( $doc->createTextNode( 'United Kingdom' ) ); $job->appendChild( $country ); //Create source/job[]/description node $desc = $doc->createElement( 'description' ); $desc->appendChild( $doc->createTextNode( $row['description'] ) ); $job->appendChild( $desc ); $src->appendChild( $job ); } echo $doc->saveXML();
Produces
xml Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8"?> <source> <publisher/> <publisherurl/> <job> <title>some job</title> <date>01/01/2009</date> <referencenumber>abc12345</referencenumber> <url><a href="http://www.*****.co.uk/info_jobid_0987654321.html" target="job0987654321">abc12345</a></url> <country>United Kingdom</country> <description>this is a description</description> </job> <job> <title>some other job</title> <date>01/02/2009</date> <referencenumber>abc1212341234345</referencenumber> <url><a href="http://www.*****.co.uk/info_jobid_09876123454321.html" target="job09876123454321">abc1212341234345</a></url> <country>United Kingdom</country> <description>this is another description</description> </job> <job> <title>some asdfasdfjob</title> <date>01/01/2037</date> <referencenumber>abc1234sdfgsdf5</referencenumber> <url><a href="http://www.*****.co.uk/info_jobid_sdfg0987654321.html" target="jobsdfg0987654321">abc1234sdfgsdf5</a></url> <country>United Kingdom</country> <description>this is the last description</description> </job> </source>
If you're question/problem is solved don't forget to mark the thread as Solved!
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
![]() |
Similar Threads
- diference between breadth fisrtsearct and depth first search (C)
- XML Menu Help - Delaying modules? (Existing Scripts)
- Please Read....Im desperate... (Troubleshooting Dead Machines)
- Holy Python Batman! It's a Noob! (Community Introductions)
- 32 bit depth image (C#)
- Desperate Call For Help!! (Windows NT / 2000 / XP)
- c++/unix fork(), fifo's, desperate need of help (C++)
- October 2004 - Mathematics: In Depth (Software Development Job Offers)
Other Threads in the PHP Forum
- Previous Thread: Please advice how the follwoing code works
- Next Thread: CSS won't completely style navigation loaded via PHP
Views: 283 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl customizableitems database date development directory display download dynamic echo email error file files folder form forms forum function functions google headmethod href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail malfunctioning menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






