Reputation: 309
I am trying to use powershell to load an xml and show all the names of the nodes without set parent. Is this possible?
Here's my code so far:
[xml]$changesXML = Get-Content $PathToXML
function ReplaceOneFormat
{
Param(
$Parent
)
foreach($child in $Parent)
{
$child.Name?
#ReplaceOneFormat $child # call this function by recursive
}
}
ReplaceOneFormat $changesXML
So, this is my example xml:
<?xml version="1.0"?>
<books>
<book author="a">1</book>
<book author="b">2</book>
<book author="c">
<page>796</page>
<language>USA</language>
<Publisher>USAFM</Publisher>
</book>
<journal>L2</journal>
<journal>
<book author="d">L3</book>
<Publisher>USAFM</Publisher>
</journal>
</books>
And I need result like this:
Node: Value:
books
book 1
book 2
book
page 796
language USA
Publisher USAFM
journal L2
journal
book L3
Publisher USAFM
Upvotes: 0
Views: 1671
Reputation: 620
I create example, to loop in all node. Hope this is help for you!
[xml]$changesXML = Get-Content $NameFile
$tmp = $changesXML.SelectNodes("//*")
$cnt = $tmp.Count
for ($i = 0; $i -lt $tmp.Count; $i++) {
"Attributes:"+$tmp.Item($i).Attributes
"BaseURI:"+$tmp.Item($i).BaseURI
"ChildNodes:"+$tmp.Item($i).ChildNodes
"InnerText:"+$tmp.Item($i).InnerText
"Name:"+$tmp.Item($i).Name
"LocalName:"+$tmp.Item($i).LocalName
"NamespaceURI:"+$tmp.Item($i).NamespaceURI
"NodeType:"+$tmp.Item($i).NodeType
"Prefix:"+$tmp.Item($i).Prefix
"**********************************************"
}
Upvotes: 2