473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Creating xml doc using php-file handling

119 100+
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:
Expand|Select|Wrap|Line Numbers
  1. <total_category>
  2.    <students_one>
  3.       <name>xxxxx</name>
  4.       <age>23</age>
  5.       <title>Student</title>
  6.       .........
  7.       .........
  8.    </students_one>
  9. </total_category>
  10.  
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
Mar 2 '09 #1
2 2470
Hi Magesh,

This code may help you.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // database constants
  3. // make sure the information is correct
  4. define("DB_SERVER", "localhost");
  5. define("DB_USER", "root");
  6. define("DB_PASS", "password");
  7. define("DB_NAME", "tutorials");
  8.  
  9. // connection to the database 
  10. $dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS) 
  11.    or die("Unable to connect to MySQL"); 
  12.  
  13. // select a database to work with 
  14. $selected = mysql_select_db(DB_NAME, $dbhandle) 
  15.    or die("Could not select examples"); 
  16.  
  17. // return all available tables 
  18. $result_tbl = mysql_query( "SHOW TABLES FROM ".DB_NAME, $dbhandle ); 
  19.  
  20. $tables = array(); 
  21. while ($row = mysql_fetch_row($result_tbl)) { 
  22.    $tables[] = $row[0]; 
  23.  
  24. $output = "<?xml version=\"1.0\" ?>\n"; 
  25. $output .= "<schema>"; 
  26.  
  27. // iterate over each table and return the fields for each table
  28. foreach ( $tables as $table ) { 
  29.    $output .= "<table name=\"$table\">"; 
  30.    $result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle ); 
  31.  
  32.    while( $row1 = mysql_fetch_row($result_fld) ) {
  33.       $output .= "<field name=\"$row1[0]\" type=\"$row1[1]\"";
  34.       $output .= ($row1[3] == "PRI") ? " primary_key=\"yes\" />" : " />";
  35.    } 
  36.  
  37.    $output .= "</table>"; 
  38.  
  39. $output .= "</schema>"; 
  40.  
  41. // tell the browser what kind of file is come in
  42. header("Content-type: text/xml"); 
  43. // print out XML that describes the schema
  44. echo $output; 
  45.  
  46. // close the connection 
  47. mysql_close($dbhandle); 
  48. ?>

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.

http://www.w3schools.com/php/php_xml_dom.asp

Good Luck...

Regards
Bharath
Mar 2 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: gonzalo briceno | last post by:
I have been using phplib for a while and I really like the framework except for form creation. Maybe it is me but I in my opinion there isn't a good way to create forms or should I say, everything...
1
by: Matthew Clubb | last post by:
Hi, I need help developing an expanding form I've decided that a use of PHP, Mysql and Javascript is the best platform for creating a selection of database interfaces which I'm trying to build...
6
by: sharma | last post by:
Hi all, I have a site called www.example.com. I would like to offer free hoting to the people and offer a site with their username like this. www.username.example.com. Please can any one here...
16
by: Mcginkel | last post by:
I am trying to find a way to load XHTML content in an Iframe. I use to do this in html by using the following code : var iframeObject = document.createElement("iframe");...
1
by: code | last post by:
Hi Grp http://www.books-download.com/?Book=1493-PHP+Hacks+%3a+Tips+%26+Tools+For+Creating+Dynamic+Websites+(Hacks) Description Programmers love its flexibility and speed; designers love its...
17
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
32
by: Joe | last post by:
I am just starting to use Object Oriented PHP coding, and I am seeing quite often the following (this example taken from a wiki): $wakka =& new Wakka($wakkaConfig); What exactly is the =&, and...
15
by: Mike Driscoll | last post by:
Hi, I've had this niggling issue from time to time. I want to create a shortcut on the user's desktop to a website that specifically loads Firefox even if Firefox is not the default browser. ...
0
by: eureka2050 | last post by:
Hi all, I am creating a radar chart containing 2 plots using jpgraph. My code is as follows:- include ("./jpgraph/src/jpgraph.php"); include ("./jpgraph/src/jpgraph_radar.php"); //...
2
by: divyac | last post by:
I have developed an address book using php and mysql.I have all the contacts list in a table with check boxes.Now that i want to create mailing labels for the checked contacts...i.e.,the arrangements...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.