现在的位置: 首页 > 综合 > 正文

asp 修改xml文件节点内容

2013年08月17日 ⁄ 综合 ⁄ 共 3946字 ⁄ 字号 评论关闭
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>

抱歉!评论已关闭.