XML 节点操作

<?xml version='1.0'?>
  <EXAMPLE>
      <CUSTOMER id="1" type="B">Mr.  Jones</CUSTOMER>
      <CUSTOMER id="2" type="C">Mr.  Johnson</CUSTOMER>
  </EXAMPLE>

<%

  Dim oXMLDoc
  Dim oXMLNode
  Dim oXMLNodeList

   Set oXMLDoc =  Server.CreateObject("MSXML2.DOMDocument.4.0")

          oXMLDoc.LoadXML(sXML)

  Set oXMLNode = oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[@id='2' or @type='C']")

         ' oXMLNode.Text will show "Mr Johnson"

  Set oXMLNode = oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[@id='1' and @type='B']")

         ' oXMLNode.Text will show "Mr Jones"
         ' oXMLNode.Attributes.getNamedItem("id").Text will show "1"
         ' oXMLNode.Attributes.getNamedItem("type").Text will show "B"
      
  ' Select a node list where id equals 1 and the type equals B or C.  
  ' In our example, only Mr.  Jones will be returned.

   Set oXMLNodeList = oXMLDoc.selectNodes("//EXAMPLE/CUSTOMER[@id='1' and (@type='B' or @type='C')]")

     ' oXMLNode.Text will show "Mr Jones"

  ' Select a node with a specific text value

   Set oXMLNode = oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[.  ='Mr.  Johnson']")

         ' oXMLNode.Text will show "Mr.  Johnson"

  ' Select node list of all nodes with CUSTOMER not equal to "EggHeadCafe"

   oXMLNodeList.selectNodes("//EXAMPLE/CUSTOMER[.  !='EggHeadCafe']")      



'  Start of MSXML4 or higher features.   If you want to use MSXML3, then
'  you'll need to set the following:  

    oXMLDoc.setProperty "SelectionLanguage", "XPath"


' Search for a substring in an attribute

Set oXMLNode = oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[substring(@type,1,2) ='DE']")      

' Search for a substring in a node value

Set oXMLNode = oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[substring(.,1,3) ='Mr.']")    

' Search for a value contained in an attribute

Set oXMLNode = oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[contains(@type,'DECEA')]")      

' Search for a contained in a node value

Set oXMLNode =  oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[contains(.,'Smith')]")    

' Search only from root of selected node.   This examples gets a list of nodes
' from a specific node not the whole XML document.   This XML document
' is too small to be a reasonable example but you get the idea.  
' The ".//" tells the parser to make the current node the "root" node of the
' document for this specific XPath query.   Without the ".", the XPath query
' would return all CUSTOMER nodes in the entire document even though
' they may be above or below the current node.

  Set oNode = oXMLDoc.selectSingleNode("//EXAMPLE")

  Set oXMLTreeList = oNode.selectNodes(".//CUSTOMER")        

         nTot = oXMLTreeList.length - 1
                
         For nCnt = 0 to nTot
                          
                Set oXMLNode = oXMLTreeList.nextNode()
                msgbox  oXMLNode.Attributes.getNamedItem("id").Text  
      
        Next



'  End of MSXML4 or higher features    
        

Set oXMLNode = nothing
Set oXMLDoc = nothing

%>

转载于:https://www.cnblogs.com/nosnowwolf/articles/709201.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值