I want to generate an XML file something similar to this
<mainnode>
<node1 name="aaa">
<subnode name1="39576" /> 1<subnode/>
<subnode><subnode/>
<subnode/>
</node1>
<node1 name="bbb">
<subnode name1="39576" /> 1<subnode/>
<subnode><subnode/>
<subnode/>
</node1>
<node1 name="aaa">
<subnode name1="39576" /> 1<subnode/>
<subnode><subnode/>
<subnode/>
</node1>
<node1 name="ccc">
<subnode name1="39576" /> 1<subnode/>
<subnode><subnode/>
<subnode/>
</node1>
<node1 name="ccc">
<subnode name1="39576" /> 1<subnode/>
<subnode><subnode/>
<subnode/>
</node1>
<node1 name="aaa">
<subnode name1="39576" /> 1<subnode/>
<subnode><subnode/>
<subnode/>
</node1>
<node1 name="ddd">
<subnode name1="66666" />
</node1>
The mainnode and the first node1 will be user defined nodename and the subnode will be name from database field name
Attributes for node1 and also subnode will be also from database fields
Help me to create the php file (xml formated)
Here is my PHP Code
<?php
include('config.php');
header('content-type:text/xml');
$table_id = 'rooms';
$query = "SELECT * FROM devices";
$dbresult = mysql_query($query);
$doc = new DomDocument('1.0');
$root = $doc->createElement('home_automation');
$root = $doc->appendChild($root);
$num = mysql_num_rows($dbresult);
$root->setAttribute('total',$num);
if($num)
{
$i=1;
while($row = mysql_fetch_assoc($dbresult))
{
$occ = $doc->createElement($table_id);
$occ = $root->appendChild($occ);
foreach ($row as $fieldname => $fieldvalue)
{
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);
if($fieldname == 'img')
$value = $doc->createTextNode("http://e-megha.electronicbits.in/img/icon/".$fieldvalue);
else $value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
$i++;
}
}
else
{
}
$xml_string = $doc->saveXML();
echo $xml_string;
?>
Now XML looks like