ASP對XML文檔的操作(DOM)

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
%>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章