Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parse a string in powershell to take a section of text between two XML tags.

Posted on 2014-04-30
10
Medium Priority
?
11,434 Views
Last Modified: 2014-04-30
Question :
Using powershell, how can I grab a string that sits between two XML tags and assign it to a variable?

Details :
I am parsing text in powershell. The line of text looks something like this:
      <JobName>Commercial</JobName>

I need to save the value between the XML tags as a variable.
In this case the word "commercial" should be saved into the variable.

Notes:
     The text lines will have leading spaces
     The XML tags and the word we want to will vary in length.
     The value between the XML tags may have a space in it.

Examples of the result we'd like :

     String =      <JobName>Commercial</JobName>
     Variable = Commercial

     String =                       <JobDescription>Residential</JobDescription>
     Variable = Residential

     String =            <JobActive>In Progress</JobActive>
     Variable = In Progress
0
Comment
Question by:JasonLattin
  • 5
  • 3
  • 2
10 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40032200
You can use a regular expression for that.

<\w+>(.*?)</\w+>

will give you what you need in $1

HTH,
Dan
0
 
LVL 29

Expert Comment

by:becraig
ID: 40032205
Also you can use the below:

$variable = (gc file.xml | select-string -pattern "JobDescription>") -replace 'JobDescription>','' | % { $_.split('<')[1]}

Open in new window

0
 

Author Comment

by:JasonLattin
ID: 40032208
Thanks for the quick answer BECRAIG.
I can't use that syntax because the XML code name is gonna vary with each line and i won't know the values in advance.
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 40032213
$text = "<JobName>Commercial</JobName>"
$a = $text -match "<\w+>(.*?)</\w+>"
$var = $Matches[1]

echo $var

Open in new window

This will print:
Commercial
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40032230
gc file.xml | %{if ($_ -match "<\w+>(.*?)</\w+>") {echo $Matches[1]}}

Open in new window

This should output all the contents that you want.
0
 

Author Closing Comment

by:JasonLattin
ID: 40032234
This worked perfectly!
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40032238
Glad I could help!
0
 
LVL 29

Expert Comment

by:becraig
ID: 40032249
Yup that last comment from Dan should do it then it will do a match for any value between tags, however will you want the content between EVERY tag in the xml doc as a variable  ?

I am trying to figure out if you are assigning dynamic variables here, based on the potential number of entries.
0
 

Author Comment

by:JasonLattin
ID: 40032276
BECRAIG...
What we are doing in actuality is reading an XML config file and using the contents of one xml tag to change the contents of other tags later in the file.
the value we look for is gonna vary so i needed Dan's code to grab the data no matter what the syntax was.
It is a beautiful fix, really. I can leverage that code to do lots of things for us, even beyond the need I have at this moment because we work with these darn config files in a dozen different ways at times.
I am vcery appreciatetive to everyone who participated.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40032277
He could use a hash and store the tag and its value.
gc file.xml | %{if ($_ -match "<(\w+)>(.*?)</\w+>") {$hash[$Matches[1]] = $Matches[2]}}
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question