本页内容是《ASP对XML文档的操作(DOM)》同时我们还提供济宁地区的网站建设,百度,雅虎,google的推广,点金,商友,等营销软件
请输入关键字:

标题搜索 内容搜索  

你现在所在的位置->首页->XML专区->ASP对XML文档的操作(DOM)

ASP对XML文档的操作(DOM)

时间:[2005-9-8 9:50:38]     作者:未知

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

【声明】本站刊载的《ASP对XML文档的操作(DOM)》一文如果有侵害你权益的情况,请联系我们。我们将及时采取措施。
QQ:44637339 Email:just6@163.com Tel:13355163107 Lining studios

友情链接

本页内容是《ASP对XML文档的操作(DOM)》 返回顶部
© 2001-2024 Lining studios 济宁速创科技有限公司, All Rights Reserved
 Processed Time:62.500ms
中华人民共和国信息产业部网站备案号: 鲁ICP备09103015号-1