php parse xml - error: StartTag: invalid element name
I have a serious problem with getting data from a sql query in php into an xml. I always get the error: StartT开发者_JS百科ag: invalid element name. The Start-Element is a Number
, I dont know if that matters?!?
Maybe you guys can help me!!
PHP:
$query = "SELECT r.object_id,
t.term_taxonomy_id,
t.term_id,
t.taxonomy,
t.description,
p.post_date_gmt,
p.post_content,
p.post_title,
p.post_excerpt
FROM wp_posts p,
wp_term_taxonomy t,
wp_term_relationships r
WHERE r.object_id= p.id
AND t.term_taxonomy_id = r.term_taxonomy_id
AND p.post_status = 'publish'
AND p.post_type = 'post'
AND to_days(p.post_date_gmt) >= to_days(now()) - 120
ORDER BY p.post_date DESC";
// DB Connect
$connection = mysql_connect($server, $user, $password);
mysql_select_db($dbName, $connection);
$res = mysql_query($query);
// XML Output
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<standing version="1.0">';
while ($row = mysql_fetch_assoc($res)){
$xml .= '
<'.$row['object_id'].'>
<term_taxonomy>'.$row['taxonomy'].'</term_taxonomy>
<description>'.$row['description'].'</description>
<post_date>'.$row['post_date_gmt'].'</post_date>
<post_content>'.$row['post_content'].'</post_content>
<post_title>'.$row['post_title'].'</pst_title>
<post_exerpt>'.$row['post_exerpt'].'</post_exerpt>
</'.$row['object_id'].'>
';
}
$xml.= '</standing>';
// Write to file
$file = 'News.xml';
if(is_file($file)) unlink($file);
$fp = fopen($file, "w+");
fwrite($fp, $xml);
fclose($fp);
mysql_close ($connection);
?>
Yes. That matters. XML should always start with one of the following:
[A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
(For good taste, it is advisable that tag names start with a descriptive word or abbreviation)
So saith w3. Let it be written. Let it be done.
You should be able to replace (and make the ID an attribute)
<object id="'.$row['object_id'].'">
/* step 2 */
</object>
XML tag names cannot be numbers, instead try
'<object id="' . $row['object_id'] . '">
It is most likely the value object_id
. You are creating an element name with this value. I assume object_id
is number or guid which is not an acceptable element name. I suggest prepending an o
to the object_id to get the element name.
精彩评论