asp 修改xml檔案節點內容

來源:互聯網
上載者:User
editBook.asp
1 傳入參數id是所修改節點在xml檔案中的下標位置時的修改方法
<%
 id=request("id")
 if IsNumeric(id)=false or isNull(id) then
response.write ("參數不正確,請返回!")
response.end
end if

isEdit=request("isEdit")
If isEdit="" Or IsNull(isEdit) then
   strSourceFile = Server.MapPath("./") & "/books.xml"
  '擷取XML檔案的路徑這雷根據你的虛擬目錄不同而不同
  Set objXML = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
 '以自由線程建立一個XML對像
 objXML.load(strSourceFile)'把XML檔案讀入記憶體
 Set objRootsite = objXML.documentElement.selectSingleNode("//books")

 bookID=objRootsite.childNodes.item(id).GetAttribute("id")
 title=objRootsite.childNodes.item(id).childNodes.item(0).text
 author=objRootsite.childNodes.item(id).childNodes.item(1).text
%>
<form name="form1" method="post" action="?isEdit=edit&id=<%=id%>">
  bookID:
  <input type="text" name="bookid" value="<%=bookID%>" size="10" readonly="true">
  &nbsp;
  bookTitle:
  <input type="text" name="title" value="<%=title%>" size="40">
  &nbsp;
  Author:
  <input type="text" name="bookAuthor" value="<%=author%>" size="40">
  <input type="submit" value="修改">
</form>
<%
End If
If isEdit="edit" Then
  bookid=request("bookid")
  title=request("title")
  author=request("bookAuthor")
  strSourceFile = Server.MapPath("./") & "/books.xml"
  '擷取XML檔案的路徑這雷根據你的虛擬目錄不同而不同
  Set objXML = Server.CreateObject("Microsoft.XMLDOM")
  '建立XML對像
  objXML.load(strSourceFile)
  '把XML檔案讀入記憶體中
  Set objRootlist = objXML.documentElement.selectSingleNode("//books")
  response.Write("51th line id:"&id)
  set rootOldNode=objRootlist.childNodes.item(id)
  brstr=chr(13)&chr(10)&chr(9)

newXMLNode=brstr&"<book id="""&bookid&""" sortID=""a"&bookid&""">"&vbnewline & _
     "<title>"&title&"</title>"&vbnewline & _
"<author>author"&author&"</author>"&vbnewline & _
"</book>"&vbnewline&vbnewline

  '根據得到的資料建立XML片段
set objXML2=Server.CreateObject("Microsoft.XMLDOM")
'建立一個新XML對像
objXML2.loadXML(newXMLNode)
'把XML版片段讀入記憶體中
set rootNewNode=objXML2.documentElement
'獲得objXML2的根節點
objRootlist.ReplaceChild rootNewNode,rootOldNode
'修改test.xml
objXML.save(strSourceFile)
'儲存test.xml檔案(因為不儲存test.xml只在記憶體中更新了)
set objXML=nothing
set objXML2=nothing
Response.Redirect("books.asp")
response.end

End if
%>

2 傳入參數id是指定節點的屬性id的值時的修改方法
<%
 id=request("id")
 if IsNumeric(id)=false or isNull(id) then
response.write ("參數不正確,請返回!")
response.end
end if

isEdit=request("isEdit")
If isEdit="" Or IsNull(isEdit) then
   strSourceFile = Server.MapPath("./") & "/books.xml"
  '擷取XML檔案的路徑這雷根據你的虛擬目錄不同而不同
  Set objXML = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
 '以自由線程建立一個XML對像
 objXML.load(strSourceFile)'把XML檔案讀入記憶體
 
  Set editObj=objXML.SelectSingleNode("//book[@id='"&id&"']")
 bookID=editObj.GetAttribute("id")
 sortID=editObj.GetAttribute("sortID")
 title=editObj.childNodes.item(0).text
 author=editObj.childNodes.item(1).text

%>
<form name="form1" method="post" action="?isEdit=edit&id=<%=id%>">
  bookID:
  <input type="text" name="bookid" value="<%=bookID%>" size="10" readonly="true">
  &nbsp;
  sortID:
  <input type="text" name="sortid" value="<%=sortID%>" size="10" readonly="true">
  <br />
  bookTitle:
  <input type="text" name="title" value="<%=title%>" size="40">
  <br />
  Author:
  <input type="text" name="bookAuthor" value="<%=author%>" size="40">
  <br />
  <input type="submit" value="修改">
</form>
<%
End If
If isEdit="edit" Then
  bookid=request("bookid")
  title=request("title")
  author=request("bookAuthor")
  strSourceFile = Server.MapPath("./") & "/books.xml"
  '擷取XML檔案的路徑這雷根據你的虛擬目錄不同而不同
  Set objXML = Server.CreateObject("Microsoft.XMLDOM")
  '建立XML對像
  objXML.load(strSourceFile)
  '把XML檔案讀入記憶體中
 
    Set editObj=objXML.SelectSingleNode("//book[@id='"&id&"']")
    editObj.Attributes.item(1).value =bookid
    editObj.childNodes.item(0).text=title
    editObj.childNodes.item(1).text=author

''修改test.xml
objXML.save(strSourceFile)
'儲存test.xml檔案
set objXML=nothing

Response.Redirect("books.asp")
response.end
End if
%>

books.xml
<?xml version="1.0" encoding="gb2312"?>
<books>
    <book id="4" sortID="a4">
        <title>author4aa</title>
        <author>author4</author>
    </book>
    <book id="5" sortID="a5">
        <title>author55</title>
        <author>author5</author>
    </book>
    <book id="6" sortID="a6">
        <title>booktitle</title>
        <author>author6</author>
    </book>
</books>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.