Jump to content

php to xml


kev wood

Recommended Posts

i am trying to convert an php mysql query into an xml formatted page which i can then have flash read and import the data.  i keep getting errors though and i have not done this before so i am stuck.

 

here is my code

 

<?php
if(!$dbconnect = mysql_connect('localhost', 'flashtest', 'flash')) {
   echo "Connection failed to the host 'localhost'.";
   exit;
} // if
if (!mysql_select_db('flashtestdb')) {
   echo "Cannot connect to database 'flashtestdb'";
   exit;
} // if

$table_id = 'articles';
$query = "SELECT * FROM $table_id";
$dbresult = mysql_query($query, $dbconnect);

// create a new XML document
$doc = new DomDocument('1.0');

// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);

// process one row at a time
while($row = mysql_fetch_assoc($dbresult)) {

  // add node for each row
  $occ = $doc->createElement($table_id);
  $occ = $root->appendChild($occ);
  
    // add a child node for each field
  foreach ($row as $fieldname => $fieldvalue) {
    $child = $doc->createElement($fieldname);
    $child = $occ->appendChild($child);
    $value = $doc->createTextNode($fieldvalue);
    $value = $child->appendChild($value);
  } // foreach
} // while

// get completed xml document
$xml_string = $doc->saveXML();

?>

 

the error i am getting are as follows

 

Warning: domdocument(): Start tag expected, '<' not found
in /home/acmeart/public_html/flashtest/getart.php on line 6


Fatal error: Call to undefined function:  createelement() in /home/acmeart/public_html/flashtest/getart.php on line 11

 

any help with this would be great

Link to comment
https://forums.phpfreaks.com/topic/127860-php-to-xml/
Share on other sites

i have got this code from following a tutorial whixh was not explained very well.

 

i have changed the DomDocument to how you stated but i am still getting errors which are as followes

 

Warning: domdocument(): Start tag expected, '<' not found in /home/acmeart/public_html/flashtest/getart.php on line 16 Fatal error: Call to undefined function: createelement() in /home/acmeart/public_html/flashtest/getart.php on line 19 

Link to comment
https://forums.phpfreaks.com/topic/127860-php-to-xml/#findComment-661987
Share on other sites

i have now got the page to work with no errors but i dont think that it is converting it to an xml document.  when the page is processed this is the output that i get.

 

  20  0000-00-00  text for the cms goes in here    19  0000-00-00  lets have fun with this and get it to work    18  0000-00-00  lets have fun with this and get it to work    17  0000-00-00  lets have fun with this and get it to work    16  0000-00-00  lets have fun with this and get it to work    14  0000-00-00  lets have fun with this and get it to work    15  0000-00-00  lets have fun with this and get it to work    13  0000-00-00  lets have fun with this and get it to work  

 

this information should be formatted to xml but it is not.

Link to comment
https://forums.phpfreaks.com/topic/127860-php-to-xml/#findComment-663761
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.