如何从特定节点读取信息

时间:2013-01-14 22:00:08

标签: xml vbscript nodes

给出以下XML文件:

 <packages>
 <package name="Library">
 <distributionPoints>
  <distributionPoint path="http://SERVER001/SMS_DP_SMSPKGD$/CEN0003B/" /> 
  <distributionPoint path="http://SERVER002/SMS_DP_SMSPKGD$/CEN0003B/" /> 
  </distributionPoints>

 <package name="SystemFiles">
 <distributionPoints>
  <distributionPoint path="http://SERVER001/SMS_DP_SMSPKGD$/CEN00262/" /> 
  <distributionPoint path="http://SERVER002/SMS_DP_SMSPKGD$/CEN00262/" /> 
  </distributionPoints>
  </package>
</packages>

我正在调用下面的函数从“SystemFiles”节点检索信息:   “HTTP:// SERVER001 / SMS_DP_SMSPKGD $ / CEN00262 /”   的 “http:// SERVER002 / SMS_DP_SMSPKGD $ / CEN00262 /”

我能够检索节点“SystemFiles”但是在尝试从其中检索信息时遇到问题。这是我的功能:

Function GetDistributionPath(packageName)
  Dim foundPackageName
  foundPackageName = false

  Set objXml = CreateObject("Microsoft.XMLDOM")
  If objXml.Load("Package.xml") Then
     WScript.Echo "loaded successfully."
  Else
     WScript.Echo "I was not able to load XML doc Package.xml!"
     WScript.Quit(1)
  End If

  Set packageNodes = objXml.documentElement.SelectNodes("//package")

  For Each packageNode in packageNodes
    If Not IsNull(packageNode.getAttribute("name"))  Then
          name = packageNode.getAttribute("name")
    End If

    If name = packageName Then
       foundPackageName = true

       'Get the DistributionPoint Paths
       'THIS CODE BELOW IS NOT WORKING <===== 

       'Set distributionPointNodes = packageNode.SelectNodes("//distributionPoint")
       'For Each distributionPointNode in distributionPointNodes
       '    distributionPointName = distributionPointNode.getAttribute("path")
       '    WScript.Echo "path: " & distributionPointName
       'Next

       Exit For
    End If
  Next
  Set objXml = Nothing

  If Not foundPackageName Then
    WScript.Echo "I could not find package name " & packageName & " in Package.xml!"
    WScript.Quit(1)
  End If   
End Function

'Other approache did not work as well
'Set objNode = objXml.selectSingleNode("packages/package[@name='SystemFiles']")
'WScript.Echo "Path: " & objNode.getAttribute("path")

1 个答案:

答案 0 :(得分:1)

您发布的.XML格式不正确(缺少</package>)。你没有说出标准战略的方式:

  Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
  Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..\data\01.xml")
  Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument")
  oXML.setProperty "SelectionLanguage", "XPath"
  oXML.async = False
  oXML.load sFSpec
  If 0 = oXML.parseError Then
     WScript.Echo oXML.xml
     WScript.Echo "-----------------"
     Dim sXPath : sXPath    = "/packages/package[@name='SystemFiles']"
     Dim ndFnd  : Set ndFnd = oXML.selectSingleNode(sXPath)
     If ndFnd Is Nothing Then
        WScript.Echo sXPath, "not found"
     Else
        WScript.Echo ndFnd.nodeName, ndFnd.getAttribute("name")
        WScript.Echo "-----------------"
        sXPath = "distributionPoints/distributionPoint"
        Dim ndlPoint  : Set ndlPoint = ndFnd.SelectNodes(sXPath)
        If 0 < ndlPoint.length Then
           Dim ndPath
           For Each ndPath In ndlPoint
               WScript.Echo ndPath.getAttribute("path")
           Next
        Else
           WScript.Echo sXPath, "not found"
        End If
     End If
  Else
     WScript.Echo oXML.parseError.reason
  End If

输出:

<packages>
        <package name="Library">
                <distributionPoints>
                        <distributionPoint path="http://SERVER001/SMS_DP_SMSPKGD$/CEN0003B/"/>
                        <distributionPoint path="http://SERVER002/SMS_DP_SMSPKGD$/CEN0003B/"/>
                </distributionPoints>
        </package>
        <package name="SystemFiles">
                <distributionPoints>
                        <distributionPoint path="http://SERVER001/SMS_DP_SMSPKGD$/CEN00262/"/>
                        <distributionPoint path="http://SERVER002/SMS_DP_SMSPKGD$/CEN00262/"/>
                </distributionPoints>
        </package>
</packages>

-----------------
package SystemFiles
-----------------
http://SERVER001/SMS_DP_SMSPKGD$/CEN00262/
http://SERVER002/SMS_DP_SMSPKGD$/CEN00262/

不适合你。因此,你必须自己检查一下,你的解决方案在哪里留下了美德之路。