Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

enter image description here

share|improve this question
    
Look at stackoverflow.com/questions/486757/… :) –  FBHY Nov 19 '13 at 10:14
    
Can you share your PHP mysql code here ? –  Krish R Nov 19 '13 at 10:17
    
@user2614506 Link you provided is not related to my question, there is no mysql used on that page and also no attributes used for child and nodes –  Siva S Tenet Nov 19 '13 at 10:17
    
@ChinnuR PHP Code is added –  Siva S Tenet Nov 19 '13 at 10:23
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.