CSDN博客

img cnlx

ASP对XML文档的操作(DOM)

发表于2004/11/3 18:15:00  850人阅读

分类: XML

XML文档内容

<?xml version="1.0" encoding="gb2312"?>
<xml>
 <note>
  <item>
   <id>1</id>
   <from>CNLX</from>
   <to>LDJ</to>
   <content>I like you very much!</content>
  </item>
 </note>
</xml>
读取文档的ASP代码
<%
 '指定XML文档路径
 strSourceFile = Server.MapPath("test.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
 response.write from
 response.write toer
 response.write content
 set objXML = nothing
%>

修改XML文档的ASP代码

<%
'创建XML文档的<item>节点,并创建其所有元素
  strSourceFile = Server.MapPath("test.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
   id=1
  End If
  
from="LDJ"
toer="CNLX"
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
%>


<%
'修改XML文档的指定节点的值
 filePath = "test.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")
      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
%>


<%
'删除指定节点
delid = Request.Querystring("delid")
sourceFile = Server.MapPath("test.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

end if
%>

阅读全文
0 0

相关文章推荐

img
取 消
img