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 PHP 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="' . $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>

Dani AI

Generated

Quick diagnosis: the feed is being produced as malformed XML. Concatenating HTML and raw database text is fragile — embedding an HTML anchor inside a <url> element and writing unescaped ampersands or the pound sign from the DB will break any XML parser unless the bytes match the declared encoding and reserved characters are escaped. As noted the characters come straight from the database, and as suggested, CDATA can be a quick workaround but it does not solve encoding mismatches and it has limits (it cannot contain the sequence ]]>).

Safer approach: let an XML API do the escaping for you. Build elements and text nodes with DOMDocument (or SimpleXML) rather than hand-rolling strings. Example:

$doc = new DOMDocument('1.0', 'UTF-8');
$source = $doc->createElement('source');
$doc->appendChild($source);

$job = $doc->createElement('job');
$title = $doc->createElement('title');
$title->appendChild($doc->createTextNode($row['position']));
$job->appendChild($title);

$url = $doc->createElement('url');
$url->appendChild($doc->createTextNode('http://example.com/info_jobid_'.$row['jobid'].'.html'));
$job->appendChild($url);

$source->appendChild($job);
$doc->save('jobfeed.xml');

Practical checklist and gotchas: 1) Make sure the XML declaration encoding matches actual bytes: save PHP files as UTF-8 without BOM and set the MySQL client charset (e.g. SET NAMES 'utf8' or use mysqli/PDO with charset). 2) Do not output any whitespace or HTML before the XML declaration. 3) Avoid embedding raw HTML anchors inside XML elements — use plain URLs or, if you must include HTML in a description, wrap only that HTML in CDATA and sanitize to remove any ]]> sequences. 4) If you must build strings manually, escape with htmlspecialchars($text, ENT_QUOTES|ENT_XML1, 'UTF-8'). 5) Validate the generated file with xmllint --noout jobfeed.xml or by loading it with DOMDocument and checking libxml_get_errors(); that will show the exact parse error to fix.

Recommended Answers

All 2 Replies

I can't see any # or &s in your code. If you have any in your variables you probably need to guard these variables using ![CDATA[...]]

Hi there, the £ and &'s come directly out of the mysql database and I can't change that because it is uploaded directly via clients.

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.