Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing xml using powershell

Tags:

powershell

I am new to powershell. I have a config XML which reads as -

<xml>     <Section name="BackendStatus">         <BEName BE="crust" Status="1" />         <BEName BE="pizza" Status="1" />         <BEName BE="pie" Status="1" />         <BEName BE="bread" Status="1" />         <BEName BE="Kulcha" Status="1" />         <BEName BE="kulfi" Status="1" />         <BEName BE="cheese" Status="1" />     </Section> </xml> 

I need to parse each element in BEName to check Status. How can this be done using PowerShell?

like image 911
haare ram Avatar asked Aug 03 '13 11:08

haare ram


People also ask

Can PowerShell parse XML?

Another way to use PowerShell to parse XML is to convert that XML to objects. The easiest way to do this is with the [xml] type accelerator. By prefixing the variable names with [xml] , PowerShell converts the original plain text XML into objects you can then work with.

How do I run an XML file in PowerShell?

One way to read an XML document in PowerShell is to typecast a variable to the type [xml]. To create this variable, we can use the Get-Content cmdlet to read all of the text in an XML document. To typecast the output of Get-Content we can simply prepend the text [xml] before the variable.

Which PowerShell command does support XML?

The Select-Xml cmdlet lets you use XPath queries to search for text in XML strings and documents. Enter an XPath query, and use the Content, Path, or Xml parameter to specify the XML to be searched.

How do I update XML in PowerShell?

To update the specific XML node using PowerShell, we first need to select that node with the attribute with SelectSingleNode() method. We have below the XML file from the link stored in SampleXml. XML on C:\Temp location. The above commands will load the XML file and select node with attribute value 'bk102'.


2 Answers

First step is to load your xml string into an XmlDocument, using powershell's unique ability to cast strings to [xml]

$doc = [xml]@' <xml>     <Section name="BackendStatus">         <BEName BE="crust" Status="1" />         <BEName BE="pizza" Status="1" />         <BEName BE="pie" Status="1" />         <BEName BE="bread" Status="1" />         <BEName BE="Kulcha" Status="1" />         <BEName BE="kulfi" Status="1" />         <BEName BE="cheese" Status="1" />     </Section> </xml> '@ 

Powershell makes it really easy to parse xml with the dot notation. This statement will produce a sequence of XmlElements for your BEName elements:

$doc.xml.Section.BEName 

Then you can pipe these objects into the where-object cmdlet to filter down the results. You can use ? as a shortcut for where

$doc.xml.Section.BEName | ? { $_.Status -eq 1 } 

The expression inside the braces will be evaluated for each XmlElement in the pipeline, and only those that have a Status of 1 will be returned. The $_ operator refers to the current object in the pipeline (an XmlElement).

If you need to do something for every object in your pipeline, you can pipe the objects into the foreach-object cmdlet, which executes a block for every object in the pipeline. % is a shortcut for foreach:

$doc.xml.Section.BEName | ? { $_.Status -eq 1 } | % { $_.BE + " is delicious" } 

Powershell is great at this stuff. It's really easy to assemble pipelines of objects, filter pipelines, and do operations on each object in the pipeline.

like image 98
Marc Litchfield Avatar answered Sep 23 '22 12:09

Marc Litchfield


If you want to start with a file you can do this

[xml]$cn = Get-Content config.xml $cn.xml.Section.BEName 

Use PowerShell to Parse an XML File

like image 33
Zombo Avatar answered Sep 22 '22 12:09

Zombo