forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ConvertTo-XmlElements.ps1
152 lines (138 loc) · 4.73 KB
/
ConvertTo-XmlElements.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<#
.SYNOPSIS
Serializes complex content into XML elements.
.INPUTS
System.Object (any object) to serialize.
.OUTPUTS
System.String for each XML-serialized value or property.
.FUNCTIONALITY
XML
.EXAMPLE
ConvertTo-XmlElements.ps1 @{html=@{body=@{p='Some text.'}}} -SkipRoot
<html>
<body>
<p>Some text.</p>
</body>
</html>
.EXAMPLE
[pscustomobject]@{UserName='zaphodb';Computer='eddie'} |ConvertTo-XmlElements.ps1
<PSCustomObject>
<UserName>zaphodb</UserName>
<Computer>eddie</Computer>
</PSCustomObject>
.EXAMPLE
'{"item": {"name": "Test", "id": 1 } }' |ConvertFrom-Json |ConvertTo-XmlElements.ps1 -SkipRoot
<item>
<name>Test</name>
<id>1</id>
</item>
#>
#Requires -Version 3
[CmdletBinding()][OutputType([string])] Param(
<#
A hash or XML element or other object to be serialized as XML elements.
Each hash value or object property value may itself be a hash or object or XML element.
#>
[Parameter(Position=0,ValueFromPipeline=$true)] $InputObject,
<#
Specifies how many levels of contained objects are included in the JSON representation.
The value can be any number from 0 to 100. The default value is 2.
ConvertTo-Json emits a warning if the number of levels in an input object exceeds this number.
#>
[ValidateRange('NonNegative')][int] $Depth = 3,
# Do not wrap the input in a root element.
[switch] $SkipRoot
)
Begin
{
$Script:OFS = [Environment]::NewLine
function ConvertTo-CompoundXmlElement
{
[CmdletBinding()] Param(
[Parameter(ValueFromPipeline=$true)][string] $PropertyName,
[switch] $IsFirst
)
Begin {if(!$IsFirst) {Write-Output ''}} # adds OFS
Process
{
$name = $PropertyName -replace '\[\]','Array' -replace '\A\W+','_' -replace '\W+','-'
Write-Output "<$name>$(ConvertTo-XmlElement $InputObject.$PropertyName -Depth ($Depth-1))</$name>"
}
End {if(!$IsFirst) {Write-Output ''}} # adds OFS
}
function ConvertTo-SimpleXmlElement
{
[CmdletBinding()] Param(
[Parameter(ValueFromPipeline=$true)][string] $PropertyName,
[switch] $IsFirst
)
Begin {if(!$IsFirst) {Write-Output ''}} # adds OFS
Process
{
$name = $PropertyName -replace '\[\]','Array' -replace '\A\W+','_' -replace '\W+','-'
Write-Output "<$name>$($InputObject.$PropertyName)</$name>"
}
End {if(!$IsFirst) {Write-Output ''}} # adds OFS
}
filter ConvertTo-XmlElement
{
[CmdletBinding()][OutputType([string])] Param(
[Parameter(Position=0,ValueFromPipeline=$true)] $InputObject,
[ValidateRange('NonNegative')][int] $Depth = 3,
[switch] $IsFirst,
[switch] $SkipRoot # not used here
)
if($null -eq $InputObject) {return '<null />'}
elseif($InputObject -is [DBNull]) {return '<DBNull />'}
elseif($InputObject -is [Array])
{ $InputObject |ConvertTo-XmlElement |ForEach-Object -Begin {''} -Process {"<Item>$_</Item>"} -End {''} }
elseif([bool],[byte],[DateTimeOffset],[decimal],[double],[float],[guid],[int],[int16],[long],[sbyte],[timespan],[uint16],[uint32],[uint64] -contains $InputObject.GetType())
{ [Xml.XmlConvert]::ToString($InputObject) }
elseif($InputObject -is [datetime])
{ [Xml.XmlConvert]::ToString($InputObject,'yyyy-MM-dd\THH:mm:ss') }
elseif($InputObject -is [string] -or $InputObject -is [char])
{ [Security.SecurityElement]::Escape($InputObject) }
elseif($InputObject -is [Hashtable] -or $InputObject -is [Collections.Specialized.OrderedDictionary])
{
if($Depth -gt 1) {$InputObject.Keys |ConvertTo-CompoundXmlElement -IsFirst:$IsFirst}
else {$InputObject.Keys |ConvertTo-SimpleXmlElement -IsFirst:$IsFirst}
}
elseif($InputObject -is [PSObject])
{
if(!@($InputObject.PSObject.Properties)) {return}
elseif($Depth -gt 1) {$InputObject.PSObject.Properties.Name |ConvertTo-CompoundXmlElement -IsFirst:$IsFirst}
else {$InputObject.PSObject.Properties.Name |ConvertTo-SimpleXmlElement -IsFirst:$IsFirst}
}
elseif($InputObject -is [xml])
{ $InputObject.OuterXml }
else
{
if(!@($InputObject.PSObject.Properties)) {return}
elseif($Depth -gt 1)
{
$InputObject |
Get-Member -MemberType Properties |
Select-Object -ExpandProperty Name |
ConvertTo-CompoundXmlElement -IsFirst:$IsFirst
}
else
{
$InputObject |
Get-Member -MemberType Properties |
Select-Object -ExpandProperty Name |
ConvertTo-SimpleXmlElement -IsFirst:$IsFirst
}
}
}
}
Process
{
if($null -eq $InputObject) {return '<null />'}
elseif($InputObject -is [DBNull]) {return '<DBNull />'}
elseif($SkipRoot) {return "$(ConvertTo-XmlElement @PSBoundParameters -IsFirst)"}
else
{
$root = $InputObject.GetType().Name -replace '\[\]','Array' -replace '\A\W+','_' -replace '\W+','-'
return "<$root>$(ConvertTo-XmlElement @PSBoundParameters)</$root>"
}
}