需要帮助使用PowerShell解析xml

时间:2015-10-05 18:01:22

标签: xml powershell

XML不适合我,我正在尝试使用PowerShell 4.0解析XML文件。我试图解析的名为post_xml_question.xml的XML文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<cluster_vips>
  <svip cluster_nme="LabCluster1">
    <node node_nme="Lab1Node1" env_cde="LAB" cluster_node_nbr="1" server_dsc="Lab Cluster Node 1" />
    <node node_nme="Lab1Node2" env_cde="LAB" cluster_node_nbr="2" server_dsc="Lab Cluster Node 2" />
    <node node_nme="Lab1Node3" env_cde="LAB" cluster_node_nbr="3" server_dsc="Lab Cluster Node 3" />
  </svip>
  <svip cluster_nme="LabCluster2">
    <node node_nme="Lab2Node1" env_cde="LAB" cluster_node_nbr="1" server_dsc="Lab VM Cluster Node 1" />
    <node node_nme="Lab2Node2" env_cde="LAB" cluster_node_nbr="2" server_dsc="Lab VM Cluster Node 2" />
  </svip>
  <svip cluster_nme="LabCluster3">
    <node node_nme="Lab3Node1" env_cde="LAB" cluster_node_nbr="1" server_dsc="Lab Cluster Node 1" />
    <node node_nme="Lab3Node2" env_cde="LAB" cluster_node_nbr="2" server_dsc="Lab Cluster Node 2" />
    <node node_nme="Lab3Node3" env_cde="LAB" cluster_node_nbr="3" server_dsc="Lab Cluster Node 3" />
  </svip>
  <svip cluster_nme="DevTestCluster">
    <node node_nme="DevTestNode1" env_cde="MIX" cluster_node_nbr="1" server_dsc="Development/Test cluster Node 1" />
    <node node_nme="DevTestNode2" env_cde="MIX" cluster_node_nbr="2" server_dsc="Development/Test cluster Node 2" />
    <node node_nme="DevTestNode3" env_cde="MIX" cluster_node_nbr="3" server_dsc="Development/Test cluster Node 3" />
  </svip>
  <svip cluster_nme="ProdCluster">
    <node node_nme="ProdNode1" env_cde="PRO" cluster_node_nbr="1" server_dsc="Production cluster Node 1" />
    <node node_nme="ProdNode2" env_cde="PRO" cluster_node_nbr="2" server_dsc="Production cluster Node 2" />
    <node node_nme="ProdNode3" env_cde="PRO" cluster_node_nbr="3" server_dsc="Production cluster Node 3" />
  </svip>
</cluster_vips>

我一直在尝试使用此链接作为指南(https://jamesmccaffrey.wordpress.com/2007/12/02/parsing-xml-files-with-powershell/),但我只是部分成功。特别是,我无法获得“get_InnerXml”代码。这是我的PowerShell代码:

    [System.Xml.XmlDocument] $xd = new-object System.Xml.XmlDocument
    $file = resolve-path("post_xml_question.xml")
    $xd.load($file)

    $nodelist = $xd.selectnodes("/cluster_vips/svip") # XPath is case sensitive

    foreach ($svip in $nodelist)
    {
        $cluster_nme = $svip.getAttribute("cluster_nme")

        $XMLNode = $svip.selectSingleNode("node")
#       $XMLNode

#       $node_nme         = $XMLNode.selectSingleNode("node_nme").get_InnerXml()
        $node_nme         = $XMLNode.node_nme
        $env_cde          = $XMLNode.env_cde
        $cluster_node_nbr = $XMLNode.cluster_node_nbr
        $server_dsc       = $XMLNode.server_dsc

        write-output "cluster_nme = $cluster_nme"
        write-output "    node_nme          = $node_nme"
        write-output "    env_cde           = $env_cde"
        write-output "    cluster_node_nbr  = $cluster_node_nbr"
        write-output "    server_dsc        = $server_dsc"
    }

    exit

这是我目前为止脚本的示例输出:

cluster_nme = LabCluster1
    node_nme          = Lab1Node1
    env_cde           = LAB
    cluster_node_nbr  = 1
    server_dsc        = Lab Cluster Node 1
cluster_nme = LabCluster2
    node_nme          = Lab2Node1
    env_cde           = LAB
    cluster_node_nbr  = 1
    server_dsc        = Lab VM Cluster Node 1
cluster_nme = LabCluster3
    node_nme          = Lab3Node1
    env_cde           = LAB
    cluster_node_nbr  = 1
    server_dsc        = Lab Cluster Node 1
cluster_nme = DevTestCluster
    node_nme          = DevTestNode1
    env_cde           = MIX
    cluster_node_nbr  = 1
    server_dsc        = Development/Test cluster Node 1
cluster_nme = ProdCluster
    node_nme          = ProdNode1
    env_cde           = PRO
    cluster_node_nbr  = 1
    server_dsc        = Production cluster Node 1

它只返回每个cluster_nme的第一个node_nme。任何人都可以帮我退回缺少的node_nme条目吗?

感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:2)

你得到一个节点,因为你写了这个:

$XMLNode = $svip.selectSingleNode("node")

因此,根据您的需求,将selectSingleNode替换为foreach循环selectNodes

foreach ($svip in $nodelist)
{
    $cluster_nme = $svip.getAttribute("cluster_nme")

    foreach ($XMLNode in $svip.selectNodes("node"))
    {
        $node_nme         = $XMLNode.node_nme
        $env_cde          = $XMLNode.env_cde
        $cluster_node_nbr = $XMLNode.cluster_node_nbr
        $server_dsc       = $XMLNode.server_dsc

        write-output "cluster_nme = $cluster_nme"
        write-output "    node_nme          = $node_nme"
        write-output "    env_cde           = $env_cde"
        write-output "    cluster_node_nbr  = $cluster_node_nbr"
        write-output "    server_dsc        = $server_dsc"
    }
}