Обикновенно ние запазваме информацията в базата данни.Но за да направим информацията по компактна можем да я запазваме под формата на XML файл.Да запазваш информация така е удобно, ако изпращаш към потребител, който не използва същата ОС като тебе.Както казах в предишните уроци XML е универсален език и може да се използва от много операционни системи.Първо ще се научим как да създаваме и съхраняваме XML файл.Примерът по-долу е кръстен "test.xml" и се съхранява в директория на сървъра.Ще използваме ASP и MS XMLDOM:
CODE1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| <%
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"
%> |
Ако отворим този запазен файл можеби ще изглежда така:
CODE1
2
3
4
5
| <?xml version="1.0"?>
<root>
<child1 />
<child2 />
</root> |
Сега ще видим това в реален HTML пример.В примера по-долу HTML файла изисква от потребителя име, държава и е-майл адрес.Информацията ще се съхранява в XML файл.
customers.html:
CODE1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <html>
<body>
<form action="saveForm.asp" method="post">
<p><b>Напишете информацията за вас.</b></p>
Имв: <input type="text" id="fname" name="fname"><br />
Фамилия: <input type="text" id="lname" name="lname"><br />
Държава: <input type="text" id="country" name="country"><br />
Е-майл адрес: <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> |
Атрибутът actoin във тага <form> ни праща към ASP файл, който изглежда така:
CODE1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
| <%
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 elementsin 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
%> |
А полученият XML файл така:
CODE1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?xml version="1.0" ?>
<customer>
<field id="firstName">
<value>Иван</value>
</field>
<field id="lastName">
<value>Иванов</value>
</field>
<field id="country">
<value>България</value>
</field>
<field id="email">
<value>mymail@myaddress.com</value>
</field>
</customer> |
Както виждате става много лесно.HTML файла показва в браузъра полетата, където трябва да се напишат имената и другата информация.ASP файла съхранява тази информация в XML файла.