Jump to content

Php Mysql And Reserved Xml Name Errors


dafunder

Recommended Posts

Hello world, I am new to XML and learning how to do XML on PHP, using data extracted from MySQL databases. My purpose is to make articles portable between pages of my website and manipulable in different situations. I use an editor to write articles, which are stored in a MySQL database. So far everything seems fine except the Reserved XML Name errors, which are rather annoying. A short sample is given as follows. The sample can be parsed and displayed properly, but it is always followed by Reserved XML Name at line **. This error disappears when the data are read from XML files. Because the server in use does not allow me to write into its HD, I cannot write data into XML files. Instead I have to write them into the database, which is hosted by another server. This error does not exist, when I use simpleXML to process the data, but simpleXML cannot handle complicated structure, like the tag <emphasis> in the sample must be displayed in italic within the paragraph (I apologise if I was wrong about simpleXML). Therefore, I use expat to parse the data. I have tried several possibilities, like changing tag names, removing blank lines, etc. None has worked. XML validators are not helpful, either, because the XML files always pass validation. The problem is in extracting data from the database. According to the line number in the error message, the problem seems to be at the end of the string. This makes me wonder maybe I have made mistakes in handling the string? Can someone help me please? Data in XML format from MySQL<?xml version="1.0"?><article><subject>Greece crisis</subject><category>politics</category><content><paragraph>Greek PM <emphasis>George Papandreou</emphasis> has said he is ready to drop a proposed referendum</paragraph></content></article> PHP extracting data from database include 'config.php'; $query = $_GET['sn']; $result = mysql_query("SELECT blog_sn, blog_time, blog_article FROM blogpost WHERE blog_sn = '". $query ."'") or die('Error, query failed to return the entry'); $row = mysql_fetch_row($result); list($blog_sn, $blog_time, $blog_article) = $row; $blog_article = stripslashes($blog_article); include 'expatXMLparser.php'; Parser$parser=xml_parser_create(); function start($parser,$element_name,$element_attrs) { switch($element_name) { case 'ARTICLE': echo '<div class="textbody">'; break; case 'SUBJECT': echo '<span style="font-size:14px; font-weight:bold">'; break; case 'CATEGORY': echo '<span style="display:none">'; break; case 'CONTENT': echo '', PHP_EOL; break; case 'PARAGRAPH': echo '<p>'; break; case 'EMPHASIS': echo '<em>'; break; case 'NAME': echo ''; break; case 'PLACENAME': echo ''; break; case 'KEYWORD': echo ''; break; case 'NOTE': echo '<sup>'; }} function stop($parser,$element_name) { switch($element_name) { case 'ARTICLE': echo '</div>', PHP_EOL; break; case 'SUBJECT': echo '</span><br />', PHP_EOL; break; case 'CATEGORY': echo '</span>'; break; case 'CONTENT': echo ''; break; case 'PARAGRAPH': echo '</p>', PHP_EOL; break; case 'EMPHASIS': echo '</em>'; break; case 'NAME': echo ''; break; case 'PLACENAME': echo ''; break; case 'KEYWORD': echo ''; break; case 'NOTE': echo '</sup>'; }} function char($parser,$data) { echo $data; } xml_set_element_handler($parser,"start","stop"); xml_set_character_data_handler($parser,"char"); while ($blog_article) { xml_parse($parser,$blog_article) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } xml_parser_free($parser);

Link to comment
Share on other sites

"Reserved XML names"?!? The only thing that exists as a requirement is that elements, attributes and processing instructions don't start with "xml". There are some additional reserved names in PHP, which might explain the SimpleXML problem. Can you at least alter the PHP files you're showing here? Could you write the thing as a string, and experiment that way, i.e.:

//$result = mysql_query("SELECT blog_sn, blog_time, blog_article FROM blogpost WHERE blog_sn = '". $query ."'")//or die('Error, query failed to return the entry');//$row = mysql_fetch_row($result);//list($blog_sn, $blog_time, $blog_article) = $row;//$blog_article = stripslashes($blog_article);$blog_article = <<<EOT<?xml version="1.0"?><article><subject>Greece crisis</subject><category>politics</category><content><paragraph>Greek PM <emphasis>George Papandreou</emphasis> has said he is ready to drop a proposed referendum</paragraph></content></article>EOT;

Also, an idea to the actual problem - try

xml_parse($parser,$blog_article, true)

and if that doesn't work, try to remove the XML prolog.It appears the problem is the parser is confused as to whether "<?xml" is a processing instruction with a reserved name.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...