Creating xml doc using php-file handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpmagesh
    New Member
    • Nov 2008
    • 119

    Creating xml doc using php-file handling

    Hi,

    I want to create a xml document using php-file handling . that i have a Database named as Total_category and table name as students_one.

    and i have fields like name, age, email, title, specification etc.. inside that students_one table. what i want to do is, i want to create a xml file with this details as
    shown below:
    Code:
    <total_category>
       <students_one>
          <name>xxxxx</name>
          <age>23</age>
          <title>Student</title>
          .........
          .........
       </students_one>
    </total_category>
    this is want i want to write in that xml file and i want to save the file as doc.xml

    some one please help me how to work over this, since i m not familiar in file handling in php. can anyone help me please

    Thanks in advance,

    Regards
    Magesh
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Google really is your friend.

    Good Article.

    Comment

    • TechiBharath
      New Member
      • Mar 2009
      • 5

      #3
      Hi Magesh,

      This code may help you.

      Code:
      <?php
      // database constants
      // make sure the information is correct
      define("DB_SERVER", "localhost");
      define("DB_USER", "root");
      define("DB_PASS", "password");
      define("DB_NAME", "tutorials");
      
      // connection to the database 
      $dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS) 
         or die("Unable to connect to MySQL"); 
      
      // select a database to work with 
      $selected = mysql_select_db(DB_NAME, $dbhandle) 
         or die("Could not select examples"); 
      
      // return all available tables 
      $result_tbl = mysql_query( "SHOW TABLES FROM ".DB_NAME, $dbhandle ); 
      
      $tables = array(); 
      while ($row = mysql_fetch_row($result_tbl)) { 
         $tables[] = $row[0]; 
      } 
      
      $output = "<?xml version=\"1.0\" ?>\n"; 
      $output .= "<schema>"; 
      
      // iterate over each table and return the fields for each table
      foreach ( $tables as $table ) { 
         $output .= "<table name=\"$table\">"; 
         $result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle ); 
      
         while( $row1 = mysql_fetch_row($result_fld) ) {
            $output .= "<field name=\"$row1[0]\" type=\"$row1[1]\"";
            $output .= ($row1[3] == "PRI") ? " primary_key=\"yes\" />" : " />";
         } 
      
         $output .= "</table>"; 
      } 
      
      $output .= "</schema>"; 
      
      // tell the browser what kind of file is come in
      header("Content-type: text/xml"); 
      // print out XML that describes the schema
      echo $output; 
      
      // close the connection 
      mysql_close($dbhandle); 
      ?>

      This code will just read the Tables from Database and write into the output string with Tag manually concatenated.

      But this is not a professional way of programming.

      Try to use XML DOM Parser with PHP.

      You can refer this for DOM With PHP.

      W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


      Good Luck...

      Regards
      Bharath
      Last edited by Markus; Mar 2 '09, 08:34 PM. Reason: Change QUOTE tags to CODE tags.

      Comment

      Working...