如果子节点值满足参数,则获取 XML 节点中的属性值 [英] Get attribute value in XML node if child node value meets parameter

查看:26
本文介绍了如果子节点值满足参数,则获取 XML 节点中的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据过滤后的子节点值 Location = 'Local' 获取属性值 Name.源 XML 如下所示:

I want to get the attribute value Name based on a filtered child node value Location = 'Local'. The source XML looks like this:

<?xml version="1.0"?>
<Root>
   <User Name="user1">
    <Element CreationDate="2014-11-16 18:05:44" ID="0a962d93-64e8-4caa-8a4d-e6e9b7df5af4" Name="ClienName1">
      <AdditionalInfo>
        <Client />
        <Number />
        <MasterMachine>LocalMachine1</MasterMachine>
       <Location>Local</Location>
       <Owner>Owner1</Owner>
      </AdditionalInfo>
    </Element>
    <Element CreationDate="2014-11-21 16:08:53" ID="65ddadf1-f7e4-467c-aedf-3d6ba5c35d1d" Name="ClienName2">
      <AdditionalInfo>
        <Client />
        <Number />
        <MasterMachine>LocalMachine1</MasterMachine>
        <Location>Local</Location>
        <Owner>Owner1</Owner>      
      </AdditionalInfo>
    </Element>
    <Element CreationDate="2014-11-24 10:00:13" ID="469479c7-a249-4bf4-a486-fc09b13ba145" Name="ClienName3">
      <AdditionalInfo>
        <Client />
        <Number />
        <MasterMachine>ServerMachine1</MasterMachine>
        <Location>Server</Location>
        <Owner>Owner2</Owner>
      </AdditionalInfo>
    </Element>
  </User>
</Root>

到目前为止我所拥有的是:

What I have so far is:

Set objXML = CreateObject("MSXML2.DOMDocument")
objXML.setProperty "SelectionLanguage", "XPath"
objXML.Load XML
Set objElementNode = objXML.selectNodes("//Root/User/Element/@Name")
Set objLocationNode = objXML.selectNodes("//Root/User/Element/@Name/AdditionalInfo [Location='Local']/Location")

For Each elementName In objElementNode
    For Each location In objLocationNode
        strMessage = elementName.text &" "& location.text &vbCr
    Next
Next
MsgBox strMessage

显然我对这个领域一无所知.到目前为止,这是基于我在网上看到的片段,但不了解如何遍历节点.

Clearly I have no clue in this realm. This is so far based on snippets I've seen online without an understanding how to traverse nodes.

推荐答案

搜索具有值为 Local 的子 Location 的 AdditionalInfo 并获取其父级的 Name 属性:

Search for AdditionalInfo having a child Location with value Local and get the Name attribute of its parent:

在代码中:

  Dim oFS      : Set oFS      = CreateObject("Scripting.FileSystemObject")
  Dim sFSpec   : sFSpec       = goFS.GetAbsolutePathName("..\testdata\xml\27135249.xml")
  Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
  objMSXML.setProperty "SelectionLanguage", "XPath"
  objMSXML.async = False
  objMSXML.load sFSpec

  If 0 = objMSXML.parseError Then
     Dim sXPath : sXPath = "Root/User/Element/AdditionalInfo[Location=""Local""]"
     Dim ndlX : Set ndlX = objMSXML.selectNodes(sXPath)
     If 0 = ndlX.length Then
        WScript.Echo sXPath, "failed"
     Else
        Dim ndX
        For Each ndX In ndlX
            WScript.Echo "Name:", ndX.parentNode.GetAttribute("Name")
        Next
     End If
  Else
     WScript.Echo objMSXML.parseError.reason
  End If

这篇关于如果子节点值满足参数,则获取 XML 节点中的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆