本文要用的xml文件code.xml:
-----------------------------------------------
<?xml version="1.0" encoding="gb2312"?>
<xml>
<note>
<item>
<id>1</id>
<from>lqt</from>
<to>yh</to>
<content>I like you very much!</content>
</item>
</note>
</xml>
------------------------------------------------
asp读取xml文件:
<%
'指定XML文档路径
strSourceFile = Server.MapPath("code.xml")
'以自由线程创建一个XML对象
Set objXML = Server.createObject("Microsoft.FreeThreadedXMLDOM")
'加载文档到内存
objXML.load(strSourceFile)
'选取note节点
Set objRootsite = objXML.documentElement.selectSingleNode("note")
'把各个节点值赋值给相应的变量
id = objRootsite.childNodes.item(0).childNodes.item(0).text
from = objRootsite.childNodes.item(0).childNodes.item(1).text
toer = objRootsite.childNodes.item(0).childNodes.item(2).text
content = objRootsite.childNodes.item(0).childNodes.item(3).text
'输出变量值
response.write id&"<br>"
response.write from&"<br>"
response.write toer&"<br>"
response.write content&"<br>"
set objXML = nothing
%>
-------------------------------------------------
asp添加xml文件节点
<%
'创建XML文档的<item>节点,并创建其所有元素
strSourceFile = Server.MapPath("code.xml")
Set objXML = Server.createObject("Microsoft.XMLDOM")
objXML.load(strSourceFile)
If objXML.parseError.ErrorCode <> 0 Then
objXML.loadXML "<?xml version=""1.0"" encoding=""gb2312"" ?><xml><note></note></xml>"
End If
Set objRootlist = objXML.documentElement.selectSingleNode("note")
If objRootlist.hasChildNodes then
id = objRootlist.lastChild.firstChild.text + 1
Else
> End If
from="yh"
toer="lqt"
content="I like you too!"
Set oListNode = objXML.documentElement.selectSingleNode("note").AppendChild(objXML.createElement("item"))
Set oDetailsNode = oListNode.appendChild(objXML.createElement("id"))
oDetailsNode.Text = id
Set oDetailsNode = oListNode.appendChild(objXML.createElement("from"))
oDetailsNode.Text = from
Set oDetailsNode = oListNode.appendChild(objXML.createElement("to"))
oDetailsNode.Text = toer
Set oDetailsNode = oListNode.appendChild(objXML.createElement("content"))
oDetailsNode.Text = content
objXML.save(strSourceFile)
Set objXML=nothing
%>
-------------------------------------------------
asp修改xml文件节点内容:
<%
'修改XML文档的指定节点的值
filePath = "code.xml"
Set objXML = server.createObject("Msxml2.DOMDocument")
objXML.async = False
loadResult = objXML.Load(server.MapPath(filePath))
If Not loadResult Then
Response.Write ("加载XML文件出错!")
Response.End
End If
i = Request.QueryString("id")
if i="" then
response.write "请输入你要修改的节点id(整数)!"
response.End()
end if
Set objNodes = objXML.selectSingleNode("xml/note/item[id ='" & i & "']")
if Not IsNull(objNodes) then
from = "LXER"
if from <> "" then
objNodes.childNodes(1).text = from
objXML.save(server.MapPath(filePath))
Set objXML=nothing
end if
Set objXML=nothing
end if
%>
-----------------------------------------------
asp删除xml文件节点:
<%
'删除指定节点
delid = Request.Querystring("id")
sourceFile = Server.MapPath("code.xml")
if delid<>"" then
Set source = Server.createObject("Msxml2.DOMDocument")
source.async = false
source.load(sourceFile)
Set currNode = source.selectSingleNode("xml/note/item[id='" & delid & "']")
if Not IsNull(currNode) then
currNode.parentNode.removeChild(currNode)
End If
source.save(sourceFile)
Response.Write("<script>alert('删除成功');</script>")
Response.end
else
Response.write "请输入你要删除的节点id(整数)!"
end if
%>
--------------------------------------------------
|