°ÑÊý¾Ý´æ´¢µ½ XML Îļþ

ͨ³££¬ÎÒÃÇÔÚÊý¾Ý¿âÖд洢Êý¾Ý¡£²»¹ý£¬Èç¹ûÏ£ÍûÊý¾ÝµÄ¿ÉÒÆÖ²ÐÔ¸üÇ¿£¬ÎÒÃÇ¿ÉÒÔ°ÑÊý¾Ý´æ´¢ XML ÎļþÖС£

´´½¨²¢±£´æ XML Îļþ

Èç¹ûÊý¾ÝÒª±»´«Ë͵½·Ç Windows ƽ̨ÉϵÄÓ¦ÓóÌÐò£¬ÄÇô°ÑÊý¾Ý±£´æÔÚ XML ÎļþÖÐÊÇÓкô¦µÄ¡£Çë¼Çס£¬XML ÓкÜÇ¿µÄ¿çƽ̨¿ÉÒÆÖ²ÐÔ£¬²¢ÇÒÊý¾ÝÎÞÐèת»»£¡

Ê×ÏÈ£¬ÎÒÃǽ«Ñ§Ï°ÈçºÎ´´½¨²¢±£´æÒ»¸ö XML Îļþ¡£ÏÂÃæµÄÕâ¸ö XML Îļþ½«±»ÃüÃûΪ "test.xml"£¬²¢±»±£´æÔÚ·þÎñÆ÷É쵀 c Ŀ¼ÖС£ÎÒÃǽ«Ê¹Óà ASP ºÍ΢ÈíµÄ XMLDOM ¶ÔÏóÀ´´´½¨²¢±£´æÕâ¸ö XML Îļþ£º

<%
Dim xmlDoc, rootEl, child1, child2, p

'´´½¨XMLÎĵµ
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")

'´´½¨¸ùÔªËØ²¢½«Ö®¼ÓÈëÎĵµ
Set rootEl = xmlDoc.createElement("root")
xmlDoc.appendChild rootEl

'´´½¨²¢¼ÓÈë×ÓÔªËØ
Set child1 = xmlDoc.createElement("child1")
Set child2 = xmlDoc.createElement("child2")
rootEl.appendChild child1
rootEl.appendChild child2

'´´½¨ XML processing instruction
'²¢°ÑËü¼Óµ½¸ùÔªËØÖ®Ç°
Set p=xmlDoc.createProcessingInstruction("xml","version='1.0'")
xmlDoc.insertBefore p,xmlDoc.childNodes(0)

'°ÑÎļþ±£´æµ½ C Ŀ¼
xmlDoc.Save "c:\test.xml"
%>

Èç¹ûÄú´ò¿ªÕâ¸ö±»±£´æµÄÎļþ£¬Ëü»áʹÕâ¸öÑù×Ó ("test.xml")£º

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

ÕæÊµµÄ±íµ¥Àý×Ó

ÏÖÔÚ£¬ÎÒÃÇ¿´Ò»¸öÕæÊµµÄ±íµ¥Àý×Ó¡£

ÎÒÃÇÊ×ÏÈ¿´Ò»ÏÂÕâ¸ö±»ÓÃÔÚÀý×ÓÖÐµÄ HTML ±íµ¥£ºÏÂÃæµÄHTML±íµ¥ÒªÇóÓû§ÊäÈëËûÃǵÄÃû×Ö¡¢¹ú¼®ÒÔ¼°µç×ÓÓʼþµØÖ·¡£ËæºóÕâЩÐÅÏ¢»á±»Ð´µ½Ò»¸ö XML Îļþ£¬ÒÔ±ã´æ´¢¡£

"customers.htm":

<html>

<body>
<form action="saveForm.asp" method="post">
<h1>ÇëÊäÈëÄúµÄÁªÏµÐÅÏ¢£º</h1>
<label>Ãû×Ö: </label>
<p><input type="text" id="firstName" name="firstName"></p>

<label>ÐÕÊÏ: </label>
<p><input type="text" id="lastName" name="lastName"></p>

<label>¹ú¼Ò: </label>
<p><input type="text" id="country" name="country"></p>

<label>Óʼþ: </label>
<p><input type="text" id="email" name="email"></p>

<p>
<input type="submit" id="btn_sub" name="btn_sub" value="Submit">
<input type="reset" id="btn_res" name="btn_res" value="Reset">
</p>
</form>
</body>

</html>

ÓÃÓÚÒÔÉÏ HTML ±íµ¥µÄ action ±»ÉèÖÃΪ "saveForm.asp"¡£"saveForm.asp" ÎļþÊÇÒ»¸ö ASP Ò³Ãæ£¬¿ÉÑ­»·±é±íµ¥Óò£¬²¢°ÑËüÃǵÄÖµ´æ´¢ÔÚÒ»¸ö XML ÎļþÖУº

<%
dim xmlDoc
dim rootEl,fieldName,fieldValue,attID
dim p,i

'Èç¹ûÓдíÎó·¢Éú£¬²»ÔÊÐí³ÌÐòÖÕÖ¹
On Error Resume Next

Set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
xmlDoc.preserveWhiteSpace=true

'´´½¨²¢ÏòÎĵµÌí¼Ó¸ùÔªËØ
Set rootEl = xmlDoc.createElement("customer")
xmlDoc.appendChild rootEl

'Ñ­»·±éÀú Form ¼¯
for i = 1 To Request.Form.Count
  '³ýÈ¥±íµ¥ÖÐµÄ button ÔªËØ
  if instr(1,Request.Form.Key(i),"btn_")=0 then
    '´´½¨ field ºÍ value ÔªËØ£¬ÒÔ¼° id ÊôÐÔ
    Set fieldName = xmlDoc.createElement("field")
    Set fieldValue = xmlDoc.createElement("value")
    Set attID = xmlDoc.createAttribute("id")
    '°Ñµ±Ç°±íµ¥ÓòµÄÃû³ÆÉèÖÃΪ id ÊôÐÔµÄÖµ
    attID.Text = Request.Form.Key(i)
    '°Ñ id ÊôÐÔÌí¼Óµ½ field ÔªËØ
    fieldName.setAttributeNode attID
    '°Ñµ±Ç°±íµ¥ÓòµÄÖµÉèÖÃΪ value ÔªËØµÄÖµ
    fieldValue.Text = Request.Form(i)
    '½« field ÔªËØ×÷Ϊ¸ùÔªËØµÄ×ÓÔªËØ½øÐÐÌí¼Ó
    rootEl.appendChild fieldName
    '½« value ÔªËØ×÷Ϊ field ÔªËØµÄ×ÓÔªËØ½øÐÐÌí¼Ó
    fieldName.appendChild fieldValue
  end if
next

'Ìí¼Ó XML processing instruction
'²¢°ÑËü¼Óµ½¸ùÔªËØÖ®Ç°
Set p = xmlDoc.createProcessingInstruction("xml","version='1.0'")
xmlDoc.insertBefore p,xmlDoc.childNodes(0)

'±£´æ XML Îļþ
xmlDoc.save "c:\Customer.xml"

'ÊÍ·ÅËùÓеĶÔÏóÒýÓÃ
set xmlDoc=nothing
set rootEl=nothing
set fieldName=nothing
set fieldValue=nothing
set attID=nothing
set p=nothing

'²âÊÔÊÇ·ñÓдíÎó·¢Éú
if err.number<>0 then
  response.write("Error: No information saved.")
else
  response.write("Your information has been saved.")
end if
%>

×¢ÊÍ£ºÈç¹ûÖ¸¶¨µÄ XML ÎļþÃûÒѾ­´æÔÚ£¬ÄǸöÎļþ»á±»¸²¸Ç£¡

XML Îļþ»áÓÉÉÏÃæµÄ´úÂëÉú³É£¬´óÖµÄÑù×ÓÊÇÕâÑùµÄ£º("Customer.xml"):

<?xml version="1.0" ?>
<customer>
  <field id="firstName">
    <value>David</value> 
  </field>
  <field id="lastName">
    <value>Smith</value> 
  </field>
  <field id="country">
    <value>China</value> 
  </field>
  <field id="email">
    <value>mymail@myaddress.com</value> 
  </field>
</customer>
VUE