JasonLattin
asked on
Parse a string in powershell to take a section of text between two XML tags.
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</JobNa me>
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</JobNa me>
Variable = Commercial
String = <JobDescription>Residentia l</JobDesc ription>
Variable = Residential
String = <JobActive>In Progress</JobActive>
Variable = In Progress
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</JobNa
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</JobNa
Variable = Commercial
String = <JobDescription>Residentia
Variable = Residential
String = <JobActive>In Progress</JobActive>
Variable = In Progress
Also you can use the below:
$variable = (gc file.xml | select-string -pattern "JobDescription>") -replace 'JobDescription>','' | % { $_.split('<')[1]}
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
gc file.xml | %{if ($_ -match "<\w+>(.*?)</\w+>") {echo $Matches[1]}}
This should output all the contents that you want.
ASKER
This worked perfectly!
Glad I could help!
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.
I am trying to figure out if you are assigning dynamic variables here, based on the potential number of entries.
ASKER
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.
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.
He could use a hash and store the tag and its value.
gc file.xml | %{if ($_ -match "<(\w+)>(.*?)</\w+>") {$hash[$Matches[1]] = $Matches[2]}}
gc file.xml | %{if ($_ -match "<(\w+)>(.*?)</\w+>") {$hash[$Matches[1]] = $Matches[2]}}
<\w+>(.*?)</\w+>
will give you what you need in $1
HTH,
Dan