1

XML is not my suit, and I am attempting to parse an XML file using PowerShell 4.0. The XML file, named post_xml_question.xml, that I am attempting to parse looks like this:

<?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>

I've been trying to use this link as somewhat of a guide (https://jamesmccaffrey.wordpress.com/2007/12/02/parsing-xml-files-with-powershell/), but I am only partially successful. In particular, I could not get the "get_InnerXml" code to work. Here is my PowerShell code:

    [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

Here is my sample output from the script so far:

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

It only returns the first node_nme for each cluster_nme. Can anyone help me return the missing node_nme entries?

Any help is appreciated. Thanks in advance.

1 Answer 1

2

You are getting one node because you wrote this:

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

So for your needs replace selectSingleNode it with foreach loop over 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"
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, it worked like a champ. It feels good to stop banging my head against the wall.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.