0

I have a serious problem with getting data from a sql query in php into an xml. I always get the error: StartTag: 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);
?>

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

Comments

1

XML tag names cannot be numbers, instead try

'<object id="' . $row['object_id'] . '">

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.