Implementation of data storage and backup using XML (1)

Source: Internet
Author: User
Tags format sql mysql query access database access mysql database linux
xml| Backup | data

XML (extensible Markup Language, Extensible Markup Language) as a data markup language is constantly arousing people's attention, XML scalability has been more and more people's attention, especially in the Cross-platform data exchange, The application of XML technology can solve this problem easily. More and more companies are announcing that they will increase their support for XML in their products, and that Microsoft's future development strategy. NET implementation is also based entirely on XML language, thus, XML will play an increasingly important role in the future data transmission and exchange development.
Introduction to XML
When it comes to XML, the advent of html,html language promotes the popularization of the Internet. At the beginning of XML's birth even now, there are many people who have such a misconception that XML will completely replace HTML, in fact, not at all. Like HTML, XML is a subset of SGML (Standard generalized Markup Language, the Universal Identity language standard), which is a standard for describing the content and structure of data on a network. Still, there is a big difference between the two.

Unlike Html,html, which provides only a common way to display information on a page (without context-related and dynamic functionality), XML gives context-related functionality to the data, inherits most of SGML's functionality, and uses less complex techniques. So we want to understand that XML is not an alternative to HTML. In the future, it is most likely that Web developers will use XML to describe the structure of the data and Web sites they need, and that HTML is used to format and display the data.

XML is not an alternative to HTML, the use of which is different, XML is designed to describe the data and fixed focus and data is what, and HTML is designed to display data and fixed focus and data is what it looks like. XML is a data-based language, it's about how to describe information, it's just about content, and HTML is about how to display information, It is more focused on styles. The traditional HTML support for content is simple, and this is when designing a large web site, make sure to use a database, because HTML doesn't provide support for a lot of content.

In addition, it is also very important that XML is extensible, that all tags and document structures in HTML are predefined, that we must use those standard HTML flags, and that XML allows us to customize our own logos and our own document structure.

How to store and back up data in XML
In fact, through the previous introduction, I believe you should understand why the use of XML for data storage and backup.

Traditional database storage is mainly through the database to achieve, the emergence of the database has brought us a great convenience to provide a fast way of searching for information, but the database also brought us some inconvenience, now the database system is particularly much, like Oracle,sql Server, Linux free software mysql and so on, they have different standards, and exist in different operating systems, in the past to exchange data between them, although feasible, but very difficult, in fact, through XML can be easily implemented.

XML can be used in database applications in the following ways:

1. Backup Database

Using XML to back up a database provides the flexibility to transfer information from the database to other platforms and database systems, and the backup database takes up very little space, providing strong support for XML in Microsoft's SQL Server2000 XML to export the records in the database to XML format, and you can import data from XML into the database through Open XML. This is not introduced here, the following describes the Web way to back up access data to an XML file, this article uses the database is the E-home kitchen system in the recipe database, the database is mainly a table food, including Id,name,info,type,number, Time field, the following is the code for backing up an Access database in a Win2000 environment: (Environment Win2000+iis)

FileName: foodbackup.asp
<title> Back up Access databases using XML </title>
<body>
<%

Dim objconn
Dim objRS
Dim objfs
Dim xmlfile
Set Objconn=server.createobject ("Adodb.connection")
objConn.Open "dsn=food;database=home;uid=;p wd=;"
objConn.Open "Driver={microsoft Access driver (*.mdb)};d bq=" +server. MapPath ("Home.mdb")//connection database, specifying the path to the database file
If MS SQL Server is:
objConn.Open "Driver={sql Server};D atabase=dbname; Server=servername; Uid=username; Pwd=password "

Set Objrs=server. CreateObject ("Adodb.recordset")
objRS. Open "Food", objconn

Set Objfs=server. CreateObject ("Scripting.FileSystemObject")
Set XMLFILE=OBJFS. CreateTextFile ("D:\food.xml")

Xmlfile.writeline "<foodlist>"

While not objRS. Eof
Xmlfile.writeline "<food><id>" &objrs ("id") & "</id><name>"
Xmlfile.writeline objRS ("name") & "</name><info>" &objrs ("info") & "</info><type > "&objrs" ("type")
Xmlfile.writeline "</type><number>" &objrs ("number") & "</number><time>" &objrs ("Time") & "</time></food>"

objRS. MoveNext
Wend

Xmlfile.writeline "</foodlist>"

objRS. Close
Set objrs=nothing

Xmlfile.close
Set xmlfile=nothing

%>
</body>



Through the above code can be implemented in Access database food table food Backup, only to be changed on the basis of this file, you can choose the database in the free selection of tables to back up. The size of the file before the backup is 3.65M, after the backup after WinZip compressed size only 225KB, only need to change the above file, you can also achieve the Oracle,excell.access,visual FoxPro and other database information backup.

We can also back up other databases via PHP, and here is the file code for backing up the MySQL database via apache+php under Linux:

File one, listtable.php (file lists all tables in the database for backup selection)
<title> backup MySQL database using XML </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body bgcolor= "#FFFFFF" text= "#000000" >
Please select the table you want to back up:
?
$con =mysql_connect (' localhost ', ' root ', ' xswlily ');
$lists =mysql_list_tables ("embed", $con);
Database Connection Code
$i = 0;
while ($i <mysql_num_rows ($lists)) {
$TB _name=mysql_tablename ($lists, $i);
echo "<a href=backup.php?table=". $TB _name. " > ". $TB _name." </a><br> ";
List all the tables
$i + +;}

?>
</body>




Document II, backup.php
<?if ($table = = "") Header ("Location:listtable.php");? ><title> backup MySQL database using XML </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body bgcolor= "#FFFFFF" text= "#000000" >
?
$con =mysql_connect (' localhost ', ' root ', ' xswlily ');
$query = "SELECT * from $table";
Database query
$result =mysql_db_query ("Embed", $query, $con);
$filestr = "<"? XML version=\ "1.0\" encoding=\ "gb2312\"? "." > ";
$filestr. = "<". $table. " S> ";
while ($row =mysql_fetch_array ($result))
List all the records
{$filestr. = "<". $table. " > ";



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.