1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?php
if(!$dbconnect = mysql_connect('localhost', 'user', 'pass')) {
echo "Connection failed to the host 'localhost'.";
exit;
} // if
if (!mysql_select_db('test')) {
echo "Cannot connect to database 'test'";
exit;
} // if
$table_id = 'some_table';
$query = "SELECT * FROM $table_id";
$dbresult = mysql_query($query, $dbconnect);
// creér un nouveau document XML
$doc = domxml_new_doc('1.0');
// créer un noeud root
$root = $doc->create_element('root');
$root = $doc->append_child($root);
//parcours des par ligne
while($row = mysql_fetch_assoc($dbresult)) {
// ajouter un noeud pour chaque ligne
$occ = $doc->create_element($table_id);
$occ = $root->append_child($occ);
// ajouter un noeud fils pour chaque champs
foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->create_element($fieldname);
$child = $occ->append_child($child);
$value = $doc->create_text_node($fieldvalue);
$value = $child->append_child($value);
} // foreach
} // whilel document
// obtenir un document xml complet
$xml_string = $doc->dump_mem(true);
echo $xml_string;
?>
************alors en principe on obtient quelque chose comme ça...
<?xml version="1.0"?>
<root>
<table1>
<column1>value1</column1>
<column2>value2</column2>
............
<columnX>valueX</columnX3>
</table1>
<table1>
............
</table1>
</root>
....NOUS PASSONS A AJOUTER DES ATTRIBUTS DANS LES NOEUDS
$child = $doc->create_element($fieldname);
$child = $outer->append_child($child);
$child->set_attribute('attr1', 'attrval1');
$child->set_attribute('attr2', 'attrval2');
$value = $doc->create_text_node($fieldvalue);
$value = $child->append_child($value);
///////en principe vous trouverez ça
<?xml version="1.0"?>
<root>
<some_table>
<column1 attr1="attrval1" attr2="attrval2">value1</column1>
<column2 attr1="attrval1" attr2="attrval2">value2</column2>
<column3 attr1="attrval1" attr2="attrval2">value3</column3>
</some_table> |