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.

<!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>
Member Avatar for diafol

Split up the xml open and close tags.

$string = "<" . "?" . "........" . "?" . ">";

do you mean like this?

$string = < . "title" . "$row[position]" . "/title" . >

Member Avatar for diafol

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

$xmlString = '<?xml version="1.0" encoding="utf-8"?>
<source>
<publisher></publisher>
<publisherurl></publisherurl>';

to

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

I may have overkilled the concatenation, but give it a go.

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>

Member Avatar for diafol

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.

$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).

Either of these should allow you to get some tidy output to the XML file.

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
$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 version="1.0" encoding="utf-8"?>
<source>
  <publisher/>
  <publisherurl/>
  <job>
    <title>some job</title>
    <date>01/01/2009</date>
    <referencenumber>abc12345</referencenumber>
    <url>&lt;a href="http://www.*****.co.uk/info_jobid_0987654321.html" target="job0987654321"&gt;abc12345&lt;/a&gt;</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>&lt;a href="http://www.*****.co.uk/info_jobid_09876123454321.html" target="job09876123454321"&gt;abc1212341234345&lt;/a&gt;</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>&lt;a href="http://www.*****.co.uk/info_jobid_sdfg0987654321.html" target="jobsdfg0987654321"&gt;abc1234sdfgsdf5&lt;/a&gt;</url>
    <country>United Kingdom</country>
    <description>this is the last description</description>
  </job>
</source>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.