| 導購 | 订阅 | 在线投稿
分享
 
 
 

將HTML表單數據存儲爲c

來源:互聯網  2006-12-16 17:29:12  評論

將表單數據存爲XML格式

通常的,ASP中表單提交的數據一般被寫入數據庫。然而,如果你想讓發送數據更爲簡便易行,那麽,可以將它書寫爲XML文件格式。這種方式對于在web上收集的數據更爲有用。因爲XML對于所用平台來說非常的簡便,所以用不著轉換數據格式。

將提交的數據寫爲XML文檔,則需要通過Microsoft XMLDOM Object創建一個新的XML文檔。Microsoft XMLDOM Object擁有一個可擴展對象庫,通過它可以創建elements,attributes以及values,通過創建的這些項目則可以組成XML文檔。我無法將整個目標模型做個完整的介紹,因爲它所包含的內容太廣泛,對于將建成的網站來說,目標模型甚至通過自身也能組建一個相對完整的部份。

在XMLDOM Object被創建出來之後,通過創建目標(此目標是關于組成XML文檔中每一層的ELEMENTS而言)XML的結構會被演示出來。接下來,會舉例說明XMLDOM是怎樣被創建出來的。創建root element之後,將它附加在XMLDOM文件上。然後創建child elements並附加在root element上,最後存儲文檔。

演示Microsoft XMLDOM 對象

$#@60;%

Dim objDom

Dim objRoot

Dim objChild1

Dim objChild2

Dim objPI

" XMLDOM 對象使用Server對象的CreateObject方法創建

Set objDom = Server.CreateObject("Microsoft.XMLDOM")

"使用XMLDOM的createElemnet方法創建一個IXMLDOMElement對象。

"createElemnet方法又一個string參數,這個string 表示該element的名稱。

返回值被傳遞到objRoot變量。objRoot表示XML文檔的根元素.。

Set objRoot = objDom.createElement("rootElement")

"Use the appendChild Method of the XMLDOM Object to add the objRoot

"Element Reference to the XML Document.

objDom.appendChild objRoot

"Now, following the same steps, you will create references to the

"child elements for the XML Document. The only difference is, when the

"child elements are appended to the document, you will call the

"appendChild Method of the IXMLDOMElement Object rather than the

"appendChild Method of the XMLDOM Object. By using the IXMLDOMElement

"to append the chi en, you are differentiating (and applying tiered

"structure to) the child elements from the root element.

Set objChild1 = objDom.createElement("childElement1")

objRoot.appendChild objChild1

Set objChild1 = objDom.createElement("childElement2")

objRoot.appendChild objChild2

"The final step to take care of before saving this document is to add

"an XML processing instruction. This is necessary so that XML parsers

"will recognize this document as an XML document.

Set objPI = objDom.createProcessingInstruction("xml","vertsion="1.0"")

"Call the insertBefore Method of the XMLDOM Object in order to insert

"the processing instruction before the root element (the zero element

"in the XMLDOM childNodes Collection).

objDom.insertBefore objPI, objDom.childNodes(0)

"Calling the Save Method of the XMLDOM Object will save this XML

"document to your disk drive. In this case, the document will be saved

"to the "c:" drive and will be named "MyXMLDoc.xml". When saving an

"XML document, if the file does not exist, it will be created. If it

"does exist, it will be overwritten.

objDom.Save "c:\MyXMLDoc.xml"

%$#@62;

||||||文檔被存檔之後,如果你再打開這個文檔,那麽則會以如下代碼列表形式出現:

MyXMLDoc.xml:

$#@60;?xml version="1.0"?$#@62;

$#@60;rootElement$#@62;

$#@60;childElement1 /$#@62;

$#@60;childElement2 /$#@62;

$#@60;/rootElement$#@62;

在"MyXMLDoc.xml"文檔中,childElement1 和 childElement2 會以空的elements形式出現。如果它們被賦值,那麽每個值都將由標記符括起來。

現在,讓我們來思考一下如何將HTML數據寫到XML文檔中去。我們已經知道該如何創建和存儲XML文檔。將一個表單數據寫到XML文檔中去的過程,現在已演變成爲Request Object"s Form Collection以及將每一個表單域的value書定到XML element value 中去的步驟重複。以上可以通過ASP來完成。

例:將數據輸送到XML

現在,我們舉一個普通的HTML表單的例子來說明。此Form有用戶名,地址,電話,以及E-MAIL等幾個域。並將這些信息寫入XML文件中並保存。

EnterContact.html:

$#@60;html$#@62;

$#@60;head$#@62;

$#@60;title$#@62;

Contact Information

$#@60;/title$#@62;

$#@60;/head$#@62;

$#@60;body$#@62;

$#@60;form action="processForm.asp" method="post"$#@62;

$#@60;h3$#@62;請輸入你的聯系方式$#@60;/h3$#@62;

First Name: $#@60;input type="text" id="firstName" name="firstName"$#@62;$#@60;br$#@62;

Last Name: $#@60;input type="text" id="lastName" name="lastName"$#@62;$#@60;br$#@62;

Address #1: $#@60;input type="text" id="address1" name="address1"$#@62;$#@60;br$#@62;

Address #2: $#@60;input type="text" id="address2" name="address2"$#@62;$#@60;br$#@62;

Phone Number: $#@60;input type="text" id="phone" name="phone"$#@62;$#@60;br$#@62;

E-Mail: $#@60;input type="text" id="email" name="email"$#@62;$#@60;br$#@62;

$#@60;input type="submit" id="btnSub" name="btnSub" value="Submit"$#@62;$#@60;br$#@62;

$#@60;/form$#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

將Form 中數據發送到processForm.asp.。這是一個ASP頁面,在這個ASP中將反複調用同一個函數將form數據寫入XML文件。

processForm.asp:

$#@60;%

"--------------------------------------------------------------------

"The "ConvertFormtoXML" Function accepts to parameters.

"strXMLFilePath - The physical path where the XML file will be saved.

"strFileName - The name of the XML file that will be saved.

"--------------------------------------------------------------------

Function ConvertFormtoXML(strXMLFilePath, strFileName)

"Declare local variables.

Dim objDom

Dim objRoot

Dim objField

Dim objFieldValue

Dim objattID

Dim objattTabOrder

Dim objPI

Dim x

"Instantiate the Microsoft XMLDOM.

Set objDom = server.CreateObject("Microsoft.XMLDOM")

objDom.preserveWhiteSpace = True

"Create your root element and append it to the XML document.

Set objRoot = objDom.createElement("contact")

objDom.appendChild objRoot

"Iterate through the Form Collection of the Request Object.

For x = 1 To Request.Form.Count

"Check to see if "btn" is in the name of the form element.

"If it is, then it is a button and we do not want to add it

"to the XML document.

If instr(1,Request.Form.Key(x),"btn") = 0 Then

"Create an element, "field".

Set objField = objDom.createElement("field")

"Create an attribute, "id".

Set objattID = objDom.createAttribute("id")

"Set the value of the id attribute equal the the name of

"the current form field.

objattID.Text = Request.Form.Key(x)

"The setAttributeNode method will append the id attribute

"to the field element.

objField.setAttributeNode objattID

"Create another attribute, "taborder". This just orders the

"elements.

Set objattTabOrder = objDom.createAttribute("taborder")

"Set the value of the taborder attribute.

objattTabOrder.Text = x

"Append the taborder attribute to the field element.

objField.setAttributeNode objattTabOrder

"Create a new element, "field_value".

Set objFieldValue = objDom.createElement("field_value")

"Set the value of the field_value element equal to

"the value of the current field in the Form Collection.

objFieldValue.Text = Request.Form(x)

"Append the field element as a child of the root element.

objRoot.appendChild objField

"Append the field_value element as a child of the field elemnt.

objField.appendChild objFieldValue

End If

Next

"Create the xml processing instruction.

Set objPI = objDom.createProcessingInstruction("xml", "version="1.0"")

"Append the processing instruction to the XML document.

objDom.insertBefore objPI, objDom.childNodes(0)

"Save the XML document.

objDom.save strXMLFilePath & "\" & strFileName

"Release all of your object references.

Set objDom = Nothing

Set objRoot = Nothing

Set objField = Nothing

Set objFieldValue = Nothing

Set objattID = Nothing

Set objattTabOrder = Nothing

Set objPI = Nothing

End Function

"Do not break on an error.

On Error Resume Next

"Call the ConvertFormtoXML function, passing in the physical path to

"save the file to and the name that you wish to use for the file.

ConvertFormtoXML "c:","Contact.xml"

"Test to see if an error occurred, if so, let the user know.

"Otherwise, tell the user that the operation was successful.

If err.number $#@60;$#@62; 0 then

Response.write("Errors occurred while saving your form submission.")

Else

Response.write("Your form submission has been saved.")

End If

%$#@62;

||||||如果你是在你自己的應用程序中使用以上代碼,請謹記一件事情,在"ConvertFormtoXML"函數已經運行的情況下,如果XML文件名已經存在,那麽,文件將會被覆蓋。在此,我建議在使用"ConvertFormtoXML"功能前,最好用隨機建立的文件名。這樣,就將有價值數據被改寫的風險降爲零。

關于XML文件的産生,舉例如下:

Contact.xml

$#@60;?xml version="1.0" ?$#@62;

$#@60;contact$#@62;

$#@60;field id="firstName" taborder="1"$#@62;

$#@60;field_value$#@62;Michael$#@60;/field_value$#@62;

$#@60;/field$#@62;

$#@60;field id="lastName" taborder="2"$#@62;

$#@60;field_value$#@62;Qualls$#@60;/field_value$#@62;

$#@60;/field$#@62;

$#@60;field id="address1" taborder="3"$#@62;

$#@60;field_value$#@62;2129 NW 27th St.$#@60;/field_value$#@62;

$#@60;/field$#@62;

$#@60;field id="address2" taborder="4"$#@62;

$#@60;field_value /$#@62;

$#@60;/field$#@62;

$#@60;field id="phone" taborder="5"$#@62;

$#@60;field_value$#@62;4055253988$#@60;/field_value$#@62;

$#@60;/field$#@62;

$#@60;field id="email" taborder="6"$#@62;

$#@60;field_value$#@62;michaelq@vertiscope.com$#@60;/field_value$#@62;

$#@60;/field$#@62;

$#@60;/contact$#@62;

我在此建議:將以上代碼複制到你個人網站服務器上的同名頁面上,並運行以上示例時。請一定要明確你使用的是對你個人服務器有效的路徑和文件名。

當你一切准備好時,請再次檢驗你的XML文件。

將表單數據存爲XML格式   通常的,ASP中表單提交的數據一般被寫入數據庫。然而,如果你想讓發送數據更爲簡便易行,那麽,可以將它書寫爲XML文件格式。這種方式對于在web上收集的數據更爲有用。因爲XML對于所用平台來說非常的簡便,所以用不著轉換數據格式。   將提交的數據寫爲XML文檔,則需要通過Microsoft XMLDOM Object創建一個新的XML文檔。Microsoft XMLDOM Object擁有一個可擴展對象庫,通過它可以創建elements,attributes以及values,通過創建的這些項目則可以組成XML文檔。我無法將整個目標模型做個完整的介紹,因爲它所包含的內容太廣泛,對于將建成的網站來說,目標模型甚至通過自身也能組建一個相對完整的部份。   在XMLDOM Object被創建出來之後,通過創建目標(此目標是關于組成XML文檔中每一層的ELEMENTS而言)XML的結構會被演示出來。接下來,會舉例說明XMLDOM是怎樣被創建出來的。創建root element之後,將它附加在XMLDOM文件上。然後創建child elements並附加在root element上,最後存儲文檔。 演示Microsoft XMLDOM 對象 $#@60;% Dim objDom Dim objRoot Dim objChild1 Dim objChild2 Dim objPI " XMLDOM 對象使用Server對象的CreateObject方法創建 Set objDom = Server.CreateObject("Microsoft.XMLDOM") "使用XMLDOM的createElemnet方法創建一個IXMLDOMElement對象。 "createElemnet方法又一個string參數,這個string 表示該element的名稱。 返回值被傳遞到objRoot變量。objRoot表示XML文檔的根元素.。 Set objRoot = objDom.createElement("rootElement") "Use the appendChild Method of the XMLDOM Object to add the objRoot "Element Reference to the XML Document. objDom.appendChild objRoot "Now, following the same steps, you will create references to the "child elements for the XML Document. The only difference is, when the "child elements are appended to the document, you will call the "appendChild Method of the IXMLDOMElement Object rather than the "appendChild Method of the XMLDOM Object. By using the IXMLDOMElement "to append the chi en, you are differentiating (and applying tiered "structure to) the child elements from the root element. Set objChild1 = objDom.createElement("childElement1") objRoot.appendChild objChild1 Set objChild1 = objDom.createElement("childElement2") objRoot.appendChild objChild2 "The final step to take care of before saving this document is to add "an XML processing instruction. This is necessary so that XML parsers "will recognize this document as an XML document. Set objPI = objDom.createProcessingInstruction("xml","vertsion="1.0"") "Call the insertBefore Method of the XMLDOM Object in order to insert "the processing instruction before the root element (the zero element "in the XMLDOM childNodes Collection). objDom.insertBefore objPI, objDom.childNodes(0) "Calling the Save Method of the XMLDOM Object will save this XML "document to your disk drive. In this case, the document will be saved "to the "c:" drive and will be named "MyXMLDoc.xml". When saving an "XML document, if the file does not exist, it will be created. If it "does exist, it will be overwritten. objDom.Save "c:\MyXMLDoc.xml" %$#@62; ||||||文檔被存檔之後,如果你再打開這個文檔,那麽則會以如下代碼列表形式出現: MyXMLDoc.xml: $#@60;?xml version="1.0"?$#@62; $#@60;rootElement$#@62; $#@60;childElement1 /$#@62; $#@60;childElement2 /$#@62; $#@60;/rootElement$#@62;   在"MyXMLDoc.xml"文檔中,childElement1 和 childElement2 會以空的elements形式出現。如果它們被賦值,那麽每個值都將由標記符括起來。   現在,讓我們來思考一下如何將HTML數據寫到XML文檔中去。我們已經知道該如何創建和存儲XML文檔。將一個表單數據寫到XML文檔中去的過程,現在已演變成爲Request Object"s Form Collection以及將每一個表單域的value書定到XML element value 中去的步驟重複。以上可以通過ASP來完成。 例:將數據輸送到XML   現在,我們舉一個普通的HTML表單的例子來說明。此Form有用戶名,地址,電話,以及E-MAIL等幾個域。並將這些信息寫入XML文件中並保存。 EnterContact.html: $#@60;html$#@62; $#@60;head$#@62; $#@60;title$#@62; Contact Information $#@60;/title$#@62; $#@60;/head$#@62; $#@60;body$#@62; $#@60;form action="processForm.asp" method="post"$#@62; $#@60;h3$#@62;請輸入你的聯系方式$#@60;/h3$#@62; First Name: $#@60;input type="text" id="firstName" name="firstName"$#@62;$#@60;br$#@62; Last Name: $#@60;input type="text" id="lastName" name="lastName"$#@62;$#@60;br$#@62; Address #1: $#@60;input type="text" id="address1" name="address1"$#@62;$#@60;br$#@62; Address #2: $#@60;input type="text" id="address2" name="address2"$#@62;$#@60;br$#@62; Phone Number: $#@60;input type="text" id="phone" name="phone"$#@62;$#@60;br$#@62; E-Mail: $#@60;input type="text" id="email" name="email"$#@62;$#@60;br$#@62; $#@60;input type="submit" id="btnSub" name="btnSub" value="Submit"$#@62;$#@60;br$#@62; $#@60;/form$#@62; $#@60;/body$#@62; $#@60;/html$#@62;   將Form 中數據發送到processForm.asp.。這是一個ASP頁面,在這個ASP中將反複調用同一個函數將form數據寫入XML文件。 processForm.asp: $#@60;% "-------------------------------------------------------------------- "The "ConvertFormtoXML" Function accepts to parameters. "strXMLFilePath - The physical path where the XML file will be saved. "strFileName - The name of the XML file that will be saved. "-------------------------------------------------------------------- Function ConvertFormtoXML(strXMLFilePath, strFileName) "Declare local variables. Dim objDom Dim objRoot Dim objField Dim objFieldValue Dim objattID Dim objattTabOrder Dim objPI Dim x "Instantiate the Microsoft XMLDOM. Set objDom = server.CreateObject("Microsoft.XMLDOM") objDom.preserveWhiteSpace = True "Create your root element and append it to the XML document. Set objRoot = objDom.createElement("contact") objDom.appendChild objRoot "Iterate through the Form Collection of the Request Object. For x = 1 To Request.Form.Count "Check to see if "btn" is in the name of the form element. "If it is, then it is a button and we do not want to add it "to the XML document. If instr(1,Request.Form.Key(x),"btn") = 0 Then "Create an element, "field". Set objField = objDom.createElement("field") "Create an attribute, "id". Set objattID = objDom.createAttribute("id") "Set the value of the id attribute equal the the name of "the current form field. objattID.Text = Request.Form.Key(x) "The setAttributeNode method will append the id attribute "to the field element. objField.setAttributeNode objattID "Create another attribute, "taborder". This just orders the "elements. Set objattTabOrder = objDom.createAttribute("taborder") "Set the value of the taborder attribute. objattTabOrder.Text = x "Append the taborder attribute to the field element. objField.setAttributeNode objattTabOrder "Create a new element, "field_value". Set objFieldValue = objDom.createElement("field_value") "Set the value of the field_value element equal to "the value of the current field in the Form Collection. objFieldValue.Text = Request.Form(x) "Append the field element as a child of the root element. objRoot.appendChild objField "Append the field_value element as a child of the field elemnt. objField.appendChild objFieldValue End If Next "Create the xml processing instruction. Set objPI = objDom.createProcessingInstruction("xml", "version="1.0"") "Append the processing instruction to the XML document. objDom.insertBefore objPI, objDom.childNodes(0) "Save the XML document. objDom.save strXMLFilePath & "\" & strFileName "Release all of your object references. Set objDom = Nothing Set objRoot = Nothing Set objField = Nothing Set objFieldValue = Nothing Set objattID = Nothing Set objattTabOrder = Nothing Set objPI = Nothing End Function "Do not break on an error. On Error Resume Next "Call the ConvertFormtoXML function, passing in the physical path to "save the file to and the name that you wish to use for the file. ConvertFormtoXML "c:","Contact.xml" "Test to see if an error occurred, if so, let the user know. "Otherwise, tell the user that the operation was successful. If err.number $#@60;$#@62; 0 then Response.write("Errors occurred while saving your form submission.") Else Response.write("Your form submission has been saved.") End If %$#@62; ||||||如果你是在你自己的應用程序中使用以上代碼,請謹記一件事情,在"ConvertFormtoXML"函數已經運行的情況下,如果XML文件名已經存在,那麽,文件將會被覆蓋。在此,我建議在使用"ConvertFormtoXML"功能前,最好用隨機建立的文件名。這樣,就將有價值數據被改寫的風險降爲零。   關于XML文件的産生,舉例如下: Contact.xml $#@60;?xml version="1.0" ?$#@62; $#@60;contact$#@62; $#@60;field id="firstName" taborder="1"$#@62; $#@60;field_value$#@62;Michael$#@60;/field_value$#@62; $#@60;/field$#@62; $#@60;field id="lastName" taborder="2"$#@62; $#@60;field_value$#@62;Qualls$#@60;/field_value$#@62; $#@60;/field$#@62; $#@60;field id="address1" taborder="3"$#@62; $#@60;field_value$#@62;2129 NW 27th St.$#@60;/field_value$#@62; $#@60;/field$#@62; $#@60;field id="address2" taborder="4"$#@62; $#@60;field_value /$#@62; $#@60;/field$#@62; $#@60;field id="phone" taborder="5"$#@62; $#@60;field_value$#@62;4055253988$#@60;/field_value$#@62; $#@60;/field$#@62; $#@60;field id="email" taborder="6"$#@62; $#@60;field_value$#@62;michaelq@vertiscope.com$#@60;/field_value$#@62; $#@60;/field$#@62; $#@60;/contact$#@62;   我在此建議:將以上代碼複制到你個人網站服務器上的同名頁面上,並運行以上示例時。請一定要明確你使用的是對你個人服務器有效的路徑和文件名。   當你一切准備好時,請再次檢驗你的XML文件。
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有