-
Notifications
You must be signed in to change notification settings - Fork 52
/
ConvertFrom-XML.ps1
96 lines (81 loc) · 3.55 KB
/
ConvertFrom-XML.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function ConvertFrom-XML {
<#
.SYNOPSIS
Function for converting XML object (XmlNode) to PSObject.
.DESCRIPTION
Function for converting XML object (XmlNode) to PSObject.
.PARAMETER node
XmlNode object (retrieved like: [xml]$xmlObject = (Get-Content C:\temp\file.xml -Raw))
.EXAMPLE
[xml]$xmlObject = (Get-Content C:\temp\file.xml -Raw)
ConvertFrom-XML $xmlObject
.NOTES
Based on https://stackoverflow.com/questions/3242995/convert-xml-to-psobject
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline)]
[System.Xml.XmlNode] $node
)
#region helper functions
function ConvertTo-PsCustomObjectFromHashtable {
param (
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)] [object[]]$hashtable
);
begin { $i = 0; }
process {
foreach ($myHashtable in $hashtable) {
if ($myHashtable.GetType().Name -eq 'hashtable') {
$output = New-Object -TypeName PsObject;
Add-Member -InputObject $output -MemberType ScriptMethod -Name AddNote -Value {
Add-Member -InputObject $this -MemberType NoteProperty -Name $args[0] -Value $args[1];
};
$myHashtable.Keys | Sort-Object | % {
$output.AddNote($_, $myHashtable.$_);
}
$output
} else {
Write-Warning "Index $i is not of type [hashtable]";
}
$i += 1;
}
}
}
#endregion helper functions
$hash = @{}
foreach ($attribute in $node.attributes) {
$hash.$($attribute.name) = $attribute.Value
}
$childNodesList = ($node.childnodes | ? { $_ -ne $null }).LocalName
foreach ($childnode in ($node.childnodes | ? { $_ -ne $null })) {
if (($childNodesList.where( { $_ -eq $childnode.LocalName })).count -gt 1) {
if (!($hash.$($childnode.LocalName))) {
Write-Verbose "ChildNode '$($childnode.LocalName)' isn't in hash. Creating empty array and storing in hash.$($childnode.LocalName)"
$hash.$($childnode.LocalName) += @()
}
if ($childnode.'#text') {
Write-Verbose "Into hash.$($childnode.LocalName) adding '$($childnode.'#text')'"
$hash.$($childnode.LocalName) += $childnode.'#text'
} else {
Write-Verbose "Into hash.$($childnode.LocalName) adding result of ConvertFrom-XML called upon '$($childnode.Name)' node object"
$hash.$($childnode.LocalName) += ConvertFrom-XML($childnode)
}
} else {
Write-Verbose "In ChildNode list ($($childNodesList -join ', ')) is only one node '$($childnode.LocalName)'"
if ($childnode.'#text') {
Write-Verbose "Into hash.$($childnode.LocalName) set '$($childnode.'#text')'"
$hash.$($childnode.LocalName) = $childnode.'#text'
} else {
Write-Verbose "Into hash.$($childnode.LocalName) set result of ConvertFrom-XML called upon '$($childnode.Name)' $($childnode.Value) object"
$hash.$($childnode.LocalName) = ConvertFrom-XML($childnode)
}
}
}
Write-Verbose "Returning hash ($($hash.Values -join ', '))"
return $hash | ConvertTo-PsCustomObjectFromHashtable
}