Dhanz Jayakumar
Dhanz Jayakumar

Reputation: 11

XML Parsing in Powershell - Specific value

I'm trying to get the values , I need to retrieve all httpurl & count under all result tag

Below is my sample XML :

<?xml version='1.0' encoding='UTF-8'?>
<results preview='0'>`enter code here`
<meta>
<fieldOrder>
<field>http_url</field>
<field>count</field>
</fieldOrder>
</meta>
  <result offset='0'>
    <field k='http_url'>
      <value h='1'><text>sample/1</text></value>
    </field>
    <field k='count'>
      <value><text>1</text></value>
    </field>
  </result>
  <result offset='1'>
    <field k='http_url'>
      <value h='1'><text>sample/2</text></value>
    </field>
    <field k='count'>
      <value><text>1</text></value>
    </field>
  </result>
  <result offset='2'>
    <field k='http_url'>
      <value h='1'><text>sample/3</text></value>
    </field>
    <field k='count'>
      <value><text>1</text></value>
    </field>
  </result>
  <result offset='3'>
    <field k='http_url'>
      <value h='1'><text>sample/4</text></value>
    </field>
    <field k='count'>
      <value><text>1</text></value>
    </field>
  </result>
  <result offset='4'>
    <field k='http_url'>
      <value h='1'><text>sample/5</text></value>
    </field>
    <field k='count'>
      <value><text>1</text></value>
    </field>
  </result>
</results>

Upvotes: 0

Views: 108

Answers (3)


$xmlDoc = [xml]@"
<?xml version="1.0" encoding="UTF-8"?>
<results preview="0">
   'enter code here'
   <meta>
      <fieldOrder>
         <field>http_url</field>
         <field>count</field>
      </fieldOrder>
   </meta>
   <result offset="0">
      <field k="http_url">
         <value h="1">
            <text>sample/1</text>
         </value>
      </field>
      <field k="count">
         <value>
            <text>1</text>
         </value>
      </field>
   </result>
   <result offset="1">
      <field k="http_url">
         <value h="1">
            <text>sample/2</text>
         </value>
      </field>
      <field k="count">
         <value>
            <text>1</text>
         </value>
      </field>
   </result>
   <result offset="2">
      <field k="http_url">
         <value h="1">
            <text>sample/3</text>
         </value>
      </field>
      <field k="count">
         <value>
            <text>1</text>
         </value>
      </field>
   </result>
   <result offset="3">
      <field k="http_url">
         <value h="1">
            <text>sample/4</text>
         </value>
      </field>
      <field k="count">
         <value>
            <text>1</text>
         </value>
      </field>
   </result>
   <result offset="4">
      <field k="http_url">
         <value h="1">
            <text>sample/5</text>
         </value>
      </field>
      <field k="count">
         <value>
            <text>1</text>
         </value>
      </field>
   </result>
</results>
"@

$xmlDoc.SelectNodes("//results/result[field[@k='http_url' or 'count']/value/text]") | ForEach-Object {
    [PSCustomObject]@{
        http_url = $_.SelectSingleNode("field[@k='http_url']/value/text")."#text"
        count = $_.SelectSingleNode("field[@k='count']/value/text")."#text"
    }
}

Upvotes: 1

Theo
Theo

Reputation: 61208

Using your example xml, you can do this to get an array of objects that have the url and the count:

[xml]$xml = Get-Content -Path 'D:\Test\test.xml' -Raw

# loop through the tags and return an array of PSObjects
# each having two properties: 'http_url' and 'count'
$xml.results.result | ForEach-Object {
    [PsCustomObject] @{
        http_url = ($_.field | Where-Object { $_.k -eq 'http_url' }).value.text
        count    = [int]($_.field | Where-Object { $_.k -eq 'count' }).value.text
    }
}

Result:

http_url count
-------- -----
sample/1     1
sample/2     1
sample/3     1
sample/4     1
sample/5     1

Upvotes: 3

Moerwald
Moerwald

Reputation: 11304

You can use the Get-Contet function to retrieve a file data to a string. Afterwards you can cast the string to an XML object via [xml]. Here is an example using a Microsoft XML example file:

> $xmlObject = [xml] (Get-Content .\books.xml)

Following command will list you all books:

> $xmlObject.catalog.book

id           : bk101
author       : Gambardella, Matthew
title        : XML Developer's Guide
genre        : Computer
price        : 44.95
publish_date : 2000-10-01
description  : An in-depth look at creating applications
                     with XML.

id           : bk102
author       : Ralls, Kim
title        : Midnight Rain
genre        : Fantasy
price        : 5.95
publish_date : 2000-12-16
description  : A former architect battles corporate zombies,
                     an evil sorceress, and her own childhood to become queen
                     of the world.

...

If you want to select certain properties you can use Select-Object:

> $xmlObject.catalog.book | Select-Object id, author

id    author
--    ------
bk101 Gambardella, Matthew
bk102 Ralls, Kim
bk103 Corets, Eva
bk104 Corets, Eva
bk105 Corets, Eva
bk106 Randall, Cynthia
bk107 Thurman, Paula
bk108 Knorr, Stefan
bk109 Kress, Peter
bk110 O'Brien, Tim
bk111 O'Brien, Tim
bk112 Galos, Mike

Upvotes: 0

Related Questions