从 XML 中获取具有特定名称的所有节点的值 [英] Getting values from all nodes with specific name from XML

查看:46
本文介绍了从 XML 中获取具有特定名称的所有节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了从 XML 获取具有特定名称的节点的所有值的最有效方法的问题.

I'm having problem with the most efficient way to get all the values of the nodes with certain name from XML.

例如:

<h1>
<MyNode>Node One</MyNode>
<h2>
    <MyNode>Note Two</MyNode>
    <DiffrentNode>I dont want that</DiffrentNode>
    <h3>
        <MyNode>Node Three</MyNode>
    </h3>
</h2>
<HHH1>
    <MyNode>Node Four</MyNode>
</HHH1>
</h1>
<g2>
    <MyNode>Node Five</MyNode>
</g2>

我想从 XML 中获取这些值:

From the XML I want to get these values:

  • 节点一
  • 节点二
  • 节点三
  • 节点四
  • 节点五

我希望能够用这样的代码做同样的事情:

I'd like to be able to do the same with code like that:

<h1>
<h2 MyArgument = "Argument One" Stuff = "I dont want that">
    <DiffrentNode>Something I dont want</DiffrentNode>
    <h3 MyArgument = "Argument Two" Stuff = "I dont want that too">
    </h3>
</h2>
<HHH1>
    <DiffrentNode>Something I dont want</DiffrentNode>
</HHH1>
</h1>
<g2 MyArgument = "Argument Three">
    <DiffrentNode>Something I dont want</DiffrentNode>
</g2>

并得到:

  • 论据一
  • 论点二
  • 论据三

有什么想法吗?到目前为止,我尝试过:

Any ideas? So far I tried doing:

$xdoc = New-Object System.Xml.XmlDocument
$file = Resolve-Path("C:\CompiledDynoview.xml")
$xdoc.load($file)
[xml] $xdoc = Get-Content $file
$xdoc.h1.MyNode
$xdoc.h1.h2.MyNode
$xdoc.h1.h3.MyNode
...

但这似乎不是最好的方法.应该有更好的东西.

But it doesn't seem to be the best way. There should be something better.

推荐答案

您在这里尝试做不同的事情,因此您将无法对这两种操作使用完全相同的代码.

You're trying to do different things here, so you won't be able to use the exact same code for both operations.

在您的第一个示例中,您要选择名称为 MyNode 的所有节点的值.选择具有 XPath 表达式 //MyNode 的节点并展开他们的 #text 属性.有多种方法可以做到这一点,例如使用 Select-Xml,正如@PetSerAl 建议的那样:

In your first example you want to select the value of all nodes with the name MyNode. Select the nodes with the XPath expression //MyNode and expand their #text property. There are various ways to do this, for instance with Select-Xml, as @PetSerAl suggested:

Select-Xml -XPath '//MyNode' -Path 'C:\path\to\first.xml' |
  Select-Object -Expand Node |
  Select-Object -Expand '#text'

或通过将文件导入为 XmlDocument 对象并使用其 SelectNodes() 方法:

or by importing the file as an XmlDocument object and using its SelectNodes() method:

[xml]$xml = Get-Content 'C:\path\to\first.xml'
$xml.SelectNodes('//MyNode') | Select-Object -Expand '#text'

在您的第二个示例中,您希望从具有此特定属性的所有节点中选择属性 MyArgument 的值.使用 XPath 表达式 //@MyArgument 选择所有属性 MyArgument,然后像以前一样扩展它们的值,如下所示:

In your second example you want to select the value of the attribute MyArgument from all nodes that have this particular attribute. Use the XPath expression //@MyArgument to select all attributes MyArgument, then expand their value as before, like this:

Select-Xml -XPath '//@MyArgument' -Path 'C:\path\to\second.xml' |
  Select-Object -Expand Node |
  Select-Object -Expand '#text'

或者像这样:

[xml]$xml = Get-Content 'C:\path\to\second.xml'
$xml.SelectNodes('//@MyArgument') | Select-Object -Expand '#text'

<小时>

附注:

$xml = New-Object System.Xml.XmlDocument
$xml.load('C:\path\to\your.xml')

[xml]$xml = Get-Content 'C:\path\to\your.xml'

做同样的事情,所以选择一个,而不是两个.

do the same thing, so use one or the other, not both.

这篇关于从 XML 中获取具有特定名称的所有节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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