w3pop.com :: ÍøÂçѧԺ :: XML :: XML ±£´æÊý¾Ý
Usually, we save data in databases. However, if we want to make the data more portable, we can store the data in an XML file.
ͨ³£Çé¿öÏ£¬ÎÒÃÇÊǰÑÊý¾Ý´æ´¢ÔÚÊý¾Ý¿âÖС£È»¶ø£¬Èç¹ûÄãÏ£ÍûÊý¾Ý¸üÈÝÒ×±»»ñÈ¡£¬ÎÒÃÇ¿ÉÒÔ°ÑÊý¾Ý±£´æÔÚXMLÎļþÖС£
Storing data in XML files is useful if the data is to be sent to applications on non-Windows platforms. Remember that XML is portable across all platforms and the data will not need to be converted!
Èç¹ûÊý¾ÝÐëÒªÒª±»·¢Ë͵½·ÇWindowsƽ̨ÉϵÄÓ¦ÓóÌÐòÖУ¬ÄÇô£¬°ÑÊý¾Ý´æ´¢ÔÚXMLÎļþÀïÊǷdz£ÓÐÓõġ£Òª¼Çס£¬XMLÔÚËùÓеIJÙ×÷ƽ̨É϶¼¿ÉÒÔ·¢ËÍÊý¾Ý£¬²¢ÇÒ£¬Êý¾ÝÊDz»ÐèÒª±»×ª»»µÄ¡£
First we will learn how to create and save an XML file. The XML file below will be named "test.xml" and will be stored in the c directory on the server. We will use ASP and Microsoft's XMLDOM object to create and save the XML file:
Ê×ÏÈ£¬ÎÒÃÇÐëҪѧϰÈçºÎ´´½¨ºÍ±£´æÒ»·ÝXMLÎļþ¡£ÏÂÃæµÄXMLÎļþ½«±»ÃüÃûΪ"test.xml"£¬²¢±»±£´æÔÚ·þÎñÆ÷µÄCÄ¿Â¼ÇøÓòÏ£¬ÎÒÃǽ«Óà ASP ºÍ Microsoft XML DOM À´´´½¨ºÍ±£´æXMLÎļþ£º
<% 'Create an XML document 'Create a root element and append it to the document 'Create and append child elements 'Add an XML processing instruction 'Save the XML file to the c directory |
If you open the saved XML file it will look something like this ("test.xml"):
Èç¹ûÄã´ò¿ª±£´æºÃµÄXMLÎļþ£¬»á¿´µ½ÏÂÊöÄÚÈÝ£º
<?xml version="1.0"?> |
Now, we will look at a real HTML form example.
ÏÖÔÚ£¬ÈÃÎÒÃÇ¿´Ò»¸öʵ¼ÊµÄHTML±íµ¥°¸Àý¡£
We will first look at the HTML form that will be used in this example: The HTML form below asks for the user's name, country, and e-mail address. This information will then be written to an XML file for storage.
Ê×ÏÈ£¬ÈÃÎÒÃÇ¿´¿´ÔÚÕâ¸ö°¸ÀýÖн«±»Ê¹ÓõÄHTML±íµ¥£ºÏÂÃæµÄHTML±íµ¥ÐèÒªÇëÇóÓû§Ãû¡¢¹ú¼®ºÍe-mail µØÖ·£¬ÕâЩÐÅÏ¢»áдÈëXMLÎļþÖв¢±»´¢´æÆðÀ´£º
"customers.htm":
<html> <body> </html> |
The action for the HTML form above is set to "saveForm.asp". The "saveForm.asp" file is an ASP page that will loop through the form fields and store their values in an XML file:
ÉÏÊöHTML±íµ¥µÄ“action”ÒѾÉèÖÃΪ"saveForm.asp"¡£"saveForm.asp"ÎļþÊÇÒ»¸öASPÒ³Ãæ£¬Ëü½«ÔÚÕû¸ö±í¸ñÇøÓòÄÚÑ»·£¬²¢°ÑËüÃǵÄÖµ±£´æµ½XMLÎļþÖУº
<% 'Do not stop if an error occurs Set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
'Create a root element and append it to the document 'Loop through the form collection 'Add an XML processing instruction 'Save the XML file 'Release all object references 'Test to see if an error occurred |
Note: If the XML file name specified already exists, it will be overwritten!
×¢Ò⣺Èç¹ûÖ¸¶¨µÄXMLÎļþÃûÒѾ´æÔÚ£¬Ëü½«»á±»¸²¸Ç¡£
The XML file that will be produced by the code above will look something like this ("Customer.xml"):
ͨ¹ýÉÏÊö´úÂëÊéдµÄXMLÎļþ£¬¾ßÌåÈçÏÂ("Customer.xml")£º
<?xml version="1.0" ?> |