2014-11-06 16 views
0

我想使用PowerShell來加載一個XML並顯示沒有設置父節點的所有節點的名稱。這可能嗎?通過PowerShell獲取所有的孩子在沒有命名標記的情況下通過powershell

這裏是我到目前爲止的代碼:

[xml]$changesXML = Get-Content $PathToXML 
function ReplaceOneFormat 
{ 
    Param(
     $Parent 
    ) 
    foreach($child in $Parent) 
    { 
     $child.Name? 
     #ReplaceOneFormat $child # call this function by recursive 
    } 
} 

ReplaceOneFormat $changesXML 

所以,這是我的示例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> 

我需要得到這樣的:

Node:  Value: 
    books  
    book  1 
    book  2 
    book   
    page  796 
    language USA 
    Publisher USAFM 
    journal  L2 
    journal 
    book  L3 
    Publisher USAFM 
+1

請提供樣本輸入和期望輸出。 – vonPryz 2014-11-06 16:48:40

+0

有趣的問題。我有同樣的任務,但我做不到,你需要什麼。 – user3231442 2014-11-07 08:19:56

回答

1

創建例如,在所有節點中循環。希望這對你有所幫助!

[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 
"**********************************************" 
} 
相關問題