当前位置: 首页 > 网络学院 > XML相关教程 > XML > XML 保存数据

XML
XML DHTML行为
XML 相关技术
XML 编辑器
XML 摘要
XML 实例
XML字符编码
xml 文档树
IE和火狐读取XML方法比较

XML 保存数据


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 682 ::
收藏到网摘: n/a

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文件中。


Create and Save 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文件里是非常有用的。要记住,XML在所有的操作平台上都可以发送数据,并且,数据是不需要被转换的。

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文件:

<%
Dim xmlDoc, rootEl, child1, child2, p
'Create an XML document
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
'Create a root element and append it to the document
Set rootEl = xmlDoc.createElement("root")
xmlDoc.appendChild rootEl
'Create and append child elements
Set child1 = xmlDoc.createElement("child1")
Set child2 = xmlDoc.createElement("child2")
rootEl.appendChild child1
rootEl.appendChild child2
'Add an XML processing instruction
'and insert it before the root element
Set p=xmlDoc.createProcessingInstruction("xml","version='1.0'")
xmlDoc.insertBefore p,xmlDoc.childNodes(0)
'Save the XML file to the c directory
xmlDoc.Save "c:test.xml"

%>

If you open the saved XML file it will look something like this ("test.xml"):
如果你打开保存好的XML文件,会看到下述内容:

<?xml version="1.0"?>
<root>
<child1 />
<child2 />
</root>

 


Real Form Example
实际表单举例

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>
<form action="saveForm.asp" method="post">
<p><b>Enter your contact information</b></p>

First Name: <input type="text" id="fname" name="fname"><br />
Last Name: <input type="text" id="lname" name="lname"><br />

Country: <input type="text" id="country" name="country"><br />
Email: <input type="text" id="email" name="email"><br />

<input type="submit" id="btn_sub" name="btn_sub" value="Submit">
<input type="reset" id="btn_res" name="btn_res" value="Reset">

</form>
</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文件中:

<%
dim xmlDoc
dim rootEl,fieldName,fieldValue,attID
dim p,i
'Do not stop if an error occurs
On Error Resume Next
Set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
xmlDoc.preserveWhiteSpace=true
'Create a root element and append it to the document
Set rootEl = xmlDoc.createElement("customer")
xmlDoc.appendChild rootEl
'Loop through the form collection
for i = 1 To Request.Form.Count
'Eliminate button elements in the form
if instr(1,Request.Form.Key(i),"btn_")=0 then
'Create a field and a value element, and an id attribute
Set fieldName = xmlDoc.createElement("field")
Set fieldValue = xmlDoc.createElement("value")
Set attID = xmlDoc.createAttribute("id")
'Set the value of the id attribute equal to the name of
'the current form field
attID.Text = Request.Form.Key(i)
'Append the id attribute to the field element
fieldName.setAttributeNode attID
'Set the value of the value element equal to
'the value of the current form field
fieldValue.Text = Request.Form(i)
'Append the field element as a child of the root element
rootEl.appendChild fieldName
'Append the value element as a child of the field element
fieldName.appendChild fieldValue
end if
next
'Add an XML processing instruction
'and insert it before the root element
Set p = xmlDoc.createProcessingInstruction("xml","version='1.0'")
xmlDoc.insertBefore p,xmlDoc.childNodes(0)
'Save the XML file
xmlDoc.save "c:Customer.xml"
'Release all object references
set xmlDoc=nothing
set rootEl=nothing
set fieldName=nothing
set fieldValue=nothing
set attID=nothing
set p=nothing
'Test to see if an error occurred
if err.number<>0 then
response.write("Error: No information saved.")
else
response.write("Your information has been saved.")
end if
%>

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" ?>
<customer>
<field id="firstName">
<value>Hege</value>
</field>

<field id="lastName">
<value>Refsnes</value>
</field>
<field id="country">
<value>Norway</value>
</field>

<field id="email">
<value>[email protected]</value>
</field>
</customer>

评论 (0) All

登陆 | 还没注册?