943,701 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 402
  • PHP RSS
Jul 20th, 2009
0

I'm way out of my depth and need desperate help.

Expand Post »
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)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
  4. <head>
  5. <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  6. <title>RAD Feed</title>
  7. </head>
  8.  
  9. <body>
  10.  
  11. <?php
  12. $username = ""; //Database Username
  13. $password = ""; //Database Password
  14. $hostname = ""; //Database Host
  15. $dbname = ""; //Database (or Catalog in MySQL parlance) name
  16. $dbh = mysql_connect($hostname, $username, $password) or die("Could not connect to database server");
  17. $selected = mysql_select_db($dbname, $dbh) or die("Database not found or problem connecting");
  18. $result = mysql_query("SELECT position, postdate, jobref, jobid, country, description FROM jobs");
  19.  
  20. // if the file exists already, delete it first to flush data
  21. $xmlfeedfile = "jobfeed.xml";
  22. $filehandle = fopen($xmlfeedfile, 'w');
  23. $itemLink = $fullurl.'/info_jobid_'.$b[jobid].'.html';
  24.  
  25. $xmlString = '<?xml version="1.0" encoding="utf-8"?>
  26. <source>
  27. <publisher></publisher>
  28. <publisherurl></publisherurl>';
  29. fwrite($filehandle, $xmlString);
  30. while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  31. $xmlString = '<job>
  32. <title>' . $row{position} . '</title>
  33. <date>' . $row{postdate} . '</date>
  34. <referencenumber>' . $row{jobref} . '</referencenumber>
  35. <url><a href="http://www.*****.co.uk' . $fullurl . '/info_jobid_' . $row{jobid} . '.html" target="job' . $row{jobid} . '">' . $row{jobref} . '</a></url>
  36. <country>United Kingdom</country>
  37. <description>' . $row{description} . '</description>
  38. </job>
  39. ';
  40. fwrite($filehandle, $xmlString);
  41. }
  42. mysql_close($dbh);
  43. fwrite($filehandle, "</source>");
  44. fclose($filehandle);
  45. ?>
  46.  
  47.  
  48.  
  49.  
  50. </body>
  51.  
  52. </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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Facte is offline Offline
29 posts
since Jul 2009
Jul 20th, 2009
0

Re: I'm way out of my depth and need desperate help.

Split up the xml open and close tags.

PHP Syntax (Toggle Plain Text)
  1. $string = "<" . "?" . "........" . "?" . ">";
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 945
Sarcastic Poster
ardav is offline Offline
6,678 posts
since Oct 2006
Jul 20th, 2009
0

Re: I'm way out of my depth and need desperate help.

do you mean like this?

$string = < . "title" . "$row[position]" . "/title" . >
Reputation Points: 10
Solved Threads: 0
Light Poster
Facte is offline Offline
29 posts
since Jul 2009
Jul 20th, 2009
0

Re: I'm way out of my depth and need desperate help.

No, your problem seems to be that the php page is trying to make sense of the <?xml .....?> tags. So change:

PHP Syntax (Toggle Plain Text)
  1. $xmlString = '<?xml version="1.0" encoding="utf-8"?>
  2. <source>
  3. <publisher></publisher>
  4. <publisherurl></publisherurl>';
to

PHP Syntax (Toggle Plain Text)
  1. $xmlString = '< ' . '?' . 'xml version="1.0" encoding="utf-8"' . '?' .'>
  2. <source>
  3. <publisher></publisher>
  4. <publisherurl></publisherurl>';

I may have overkilled the concatenation, but give it a go.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 945
Sarcastic Poster
ardav is offline Offline
6,678 posts
since Oct 2006
Jul 20th, 2009
0

Re: I'm way out of my depth and need desperate help.

Overkill or not that seemed to work, it gets stuck on reading from a <div> now though

An invalid character was found in text content. Error processing resource 'http://www.editorialjobs.co.uk/rad.xml'. Line 1...

<description><div>
Reputation Points: 10
Solved Threads: 0
Light Poster
Facte is offline Offline
29 posts
since Jul 2009
Jul 20th, 2009
0

Re: I'm way out of my depth and need desperate help.

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.

PHP Syntax (Toggle Plain Text)
  1. $pos = stripslashes($row['position']);
  2. $date = stripslashes($row['postdate']);
  3. $ref = stripslashes($row['jobref']);
  4. $desc = stripslashes($row['jobref']);
  5. $jid = stripslashes($row['jobid']);
  6.  
  7. //OR $desc = stripslashes(strip_tags($row['desc'])); ETC, ETC
  8.  
  9. $xmlString = "<job>\n\t<title>{$pos}</title>\n\t<date>{$date}</date>\n\t
  10. <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
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 945
Sarcastic Poster
ardav is offline Offline
6,678 posts
since Oct 2006
Jul 20th, 2009
0

Re: I'm way out of my depth and need desperate help.

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

php Syntax (Toggle Plain Text)
  1. <?php
  2. $result = array();
  3. $result[] = array(
  4. 'position' => 'some job',
  5. 'postdate' => '01/01/2009',
  6. 'jobref' => 'abc12345',
  7. 'jobid' => '0987654321',
  8. 'description' => 'this is a description'
  9. );
  10.  
  11. $result[] = array(
  12. 'position' => 'some other job',
  13. 'postdate' => '01/02/2009',
  14. 'jobref' => 'abc1212341234345',
  15. 'jobid' => '09876123454321',
  16. 'description' => 'this is another description'
  17. );
  18.  
  19. $result[] = array(
  20. 'position' => 'some asdfasdfjob',
  21. 'postdate' => '01/01/2037',
  22. 'jobref' => 'abc1234sdfgsdf5',
  23. 'jobid' => 'sdfg0987654321',
  24. 'description' => 'this is the last description'
  25. );
  26.  
  27. $fullurl = '';
  28.  
  29.  
  30. $doc = new DOMDocument('1.0', 'utf-8');
  31. $doc->formatOutput = true;
  32.  
  33. //Create the source node
  34. $src = $doc->createElement( 'source' );
  35. $doc->appendChild( $src );
  36.  
  37. //create the source/publisher node
  38. $pub = $doc->createElement( 'publisher' );
  39. $src->appendChild( $pub );
  40.  
  41. //create the source/publisherurl node
  42. $pubUrl = $doc->createElement( 'publisherurl' );
  43. $src->appendChild( $pubUrl );
  44.  
  45. //Loop over sql results and create source/job nodes
  46. foreach( $result as $row )
  47. {
  48.  
  49. //Create /source/job node
  50. $job = $doc->createElement( 'job' );
  51.  
  52. //Create source/job[]/title node
  53. $title = $doc->createElement( 'title' );
  54. $title->appendChild( $doc->createTextNode( $row['position'] ) );
  55. $job->appendChild( $title );
  56.  
  57. //Create source/job[]/date node
  58. $date = $doc->createElement( 'date' );
  59. $date->appendChild( $doc->createTextNode( $row['postdate'] ) );
  60. $job->appendChild( $date );
  61.  
  62. //Create source/job[]/referencenumber node
  63. $ref = $doc->createElement( 'referencenumber' );
  64. $ref->appendChild( $doc->createTextNode( $row['jobref'] ) );
  65. $job->appendChild( $ref );
  66.  
  67. //Create source/job[]/url node
  68. $url = $doc->createElement( 'url' );
  69. $url->appendChild( $doc->createTextNode( '<a href="http://www.*****.co.uk' . $fullurl . '/info_jobid_' . $row['jobid'] . '.html" target="job' . $row['jobid'] . '">' . $row['jobref'] . '</a>' ) );
  70. $job->appendChild( $url );
  71.  
  72. //Create source/job[]/country node
  73. $country = $doc->createElement( 'country' );
  74. $country->appendChild( $doc->createTextNode( 'United Kingdom' ) );
  75. $job->appendChild( $country );
  76.  
  77. //Create source/job[]/description node
  78. $desc = $doc->createElement( 'description' );
  79. $desc->appendChild( $doc->createTextNode( $row['description'] ) );
  80. $job->appendChild( $desc );
  81.  
  82. $src->appendChild( $job );
  83. }
  84.  
  85. echo $doc->saveXML();

Produces
xml Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <source>
  3. <publisher/>
  4. <publisherurl/>
  5. <job>
  6. <title>some job</title>
  7. <date>01/01/2009</date>
  8. <referencenumber>abc12345</referencenumber>
  9. <url>&lt;a href="http://www.*****.co.uk/info_jobid_0987654321.html" target="job0987654321"&gt;abc12345&lt;/a&gt;</url>
  10. <country>United Kingdom</country>
  11. <description>this is a description</description>
  12. </job>
  13. <job>
  14. <title>some other job</title>
  15. <date>01/02/2009</date>
  16. <referencenumber>abc1212341234345</referencenumber>
  17. <url>&lt;a href="http://www.*****.co.uk/info_jobid_09876123454321.html" target="job09876123454321"&gt;abc1212341234345&lt;/a&gt;</url>
  18. <country>United Kingdom</country>
  19. <description>this is another description</description>
  20. </job>
  21. <job>
  22. <title>some asdfasdfjob</title>
  23. <date>01/01/2037</date>
  24. <referencenumber>abc1234sdfgsdf5</referencenumber>
  25. <url>&lt;a href="http://www.*****.co.uk/info_jobid_sdfg0987654321.html" target="jobsdfg0987654321"&gt;abc1234sdfgsdf5&lt;/a&gt;</url>
  26. <country>United Kingdom</country>
  27. <description>this is the last description</description>
  28. </job>
  29. </source>
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Please advice how the follwoing code works
Next Thread in PHP Forum Timeline: CSS won't completely style navigation loaded via PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC