获取XML节点的不同属性

时间:2012-11-22 14:29:00

标签: asp-classic vbscript

我一直试图从中获取不同的属性值 XML: dim i,xmlOx,arr(100) “xmlOx”具有以下xml结构

  <root>
    <a x="Animal" y="Bird"/>
    <a x="Animal" y="Bird"/>
    <a x="Country" y="Bird"/>
    <a x="Animal" y="Bird"/>
    </root>

ASP:

for i=0  xmlOx.selectNodes("a").length-1
 arr(i)=xmlOx(i).selectNodes("a").getAttribute("x")
next

就像,这里我ve to get "x" attribute values but I don需要重复。然后我需要在vbscript中将值添加到数组中。

有人告诉我我们是怎么做到的吗?

1 个答案:

答案 0 :(得分:0)

理论是here

代码:

  Dim sXml : sXml = Join(Array( _
      "<root>" _
    , "<a x=""Animal"" y=""Bird""/>" _
    , "<a x=""Animal"" y=""Bird""/>" _
    , "<a x=""Country"" y=""Bird""/>" _
    , "<a x=""Animal"" y=""Bird""/>" _
    , "</root>" _
  ))
  Dim oXDoc : Set oXDoc = CreateObject("Msxml2.DOMDocument")
  oXDoc.loadXml sXml
  WScript.Echo oXDoc.xml

  Dim xObjXml  : Set xObjXml  = oXDoc.documentElement.childNodes
  Dim dicAttrs : Set dicAttrs = CreateObject("Scripting.Dictionary")
  Dim i
  For i = 0 To xObjXml.length - 1
      Dim a : a = xObjXml(i).getAttribute("x")
      dicAttrs(a) = dicAttrs(a) + 1
  Next
  Dim aAttrs : aAttrs = dicAttrs.Keys()
  WScript.Echo Join(aAttrs)

输出:

<root>
        <a x="Animal" y="Bird"/>
        <a x="Animal" y="Bird"/>
        <a x="Country" y="Bird"/>
        <a x="Animal" y="Bird"/>
</root>

Animal Country