解析Dom节点和属性

该博客主要使用VBScript处理XML数据。通过创建字典和XMLDOM对象加载XML文件,遍历节点和属性,将节点名称和属性信息存储在字典中,最后将数据展示在设置好样式的HTML表格里,若文件加载失败会给出提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<style>
table
{
 border-collapse: collapse;
 border-width: 4;
 border-style: double;
 border-color:#15336F;
 font-size:12px;
}
body
{
 font-size:12px;
}
</style>
 
 <table border="1">
 <tr>
  <td> 节点名称 </td><td> 属性名称 </td>
 </tr>
 <script language="vbScript" >
 
  public dic,doc
  set dic = CreateObject("Scripting.Dictionary") 
  set doc = CreateObject("Microsoft.XMLDOM")  
  doc.async=False
 
  if not doc.load("cmp.xml") then
   alert "文件加载失败,请检查文件是否存在!" 
  else
  Set rootNode = doc.DocumentElement
  getnode rootNode


  for each kkk in dic.Keys
   document.write  "<tr><td>" & kkk & "</td><td>"
   if len(trim(dic.Item(kkk))) > 0 then
    document.write  dic.Item(kkk) & "</tr>"
   else
    document.write  "&nbsp;" & "</tr>"
   end if
  next
  end if
 
 sub getnode(myNode)  
  for each childNode in myNode.childNodes
   if childNode.nodeName<>"#text" then
    if not(dic.Exists(childNode.nodeName)) then
     if childNode.Attributes.length>0 then
      dic.Add childNode.nodeName,  makeString(childNode.Attributes )
     else
      dic.Add childNode.nodeName,""
     end if
    else
     dic.Item(childNode.nodeName) = exclude(dic.Item(childNode.nodeName),childNode.Attributes )
    end if
   end if
   if childNode.childNodes.length > 0 then getnode childNode
  next 
 end sub 

 function exclude(myString,collection)
  dim sss,ccc,kkk,d
  set d = CreateObject("Scripting.Dictionary")

  for each sss in split(myString,",")
   if not d.Exists(sss) then d.Add sss,sss  
  next
  
  for each ccc in collection
   if not d.Exists(ccc.name) then d.Add ccc.name,ccc.name  
  next

  for each kkk in d.Keys
   exclude = exclude  & kkk  & " "
  next

  exclude = replace(trim(exclude)," ",",")
 end function
 
 function makeString(collection)
  dim ccc
  makeString = " "
  
  for each ccc in  collection
   makeString = makeString & ccc.name & " "
  next
  makeString = replace(trim(makeString)," ",",")
 end function
 
 </script>
 </table>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值