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

Reply

Join Date: Jul 2009
Posts: 23
Reputation: Facte is an unknown quantity at this point 
Solved Threads: 0
Facte's Avatar
Facte Facte is offline Offline
Newbie Poster

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

 
0
  #1
Jul 20th, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,092
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

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

 
0
  #2
Jul 20th, 2009
Split up the xml open and close tags.

  1. $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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 23
Reputation: Facte is an unknown quantity at this point 
Solved Threads: 0
Facte's Avatar
Facte Facte is offline Offline
Newbie Poster

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

 
0
  #3
Jul 20th, 2009
do you mean like this?

$string = < . "title" . "$row[position]" . "/title" . >
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,092
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

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

 
0
  #4
Jul 20th, 2009
No, your problem seems to be that the php page is trying to make sense of the <?xml .....?> tags. So change:

  1. $xmlString = '<?xml version="1.0" encoding="utf-8"?>
  2. <source>
  3. <publisher></publisher>
  4. <publisherurl></publisherurl>';
to

  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.
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 23
Reputation: Facte is an unknown quantity at this point 
Solved Threads: 0
Facte's Avatar
Facte Facte is offline Offline
Newbie Poster

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

 
0
  #5
Jul 20th, 2009
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>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,092
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

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

 
0
  #6
Jul 20th, 2009
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.

  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
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 149
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

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

 
0
  #7
Jul 20th, 2009
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

  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
  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>
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 283 | Replies: 6
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC