PowerShell 技能连载 - XML 处理技巧

在 PowerShell 中处理 XML 数据是一项常见任务,特别是在配置管理和数据交换场景中。本文将介绍一些实用的 XML 处理技巧。

首先,让我们看看 XML 的基本操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 创建 XML 文档
$xml = [xml]@"
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="ServerName" value="localhost" />
<add key="Port" value="8080" />
</appSettings>
<database>
<connectionString>Server=localhost;Database=MyDB;Trusted_Connection=True;</connectionString>
</database>
</configuration>
"@

# 访问 XML 节点
Write-Host "`n服务器名称:$($xml.configuration.appSettings.add[0].value)"
Write-Host "端口号:$($xml.configuration.appSettings.add[1].value)"

XML 节点操作:

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
# 创建 XML 节点操作函数
function Update-XMLNode {
param(
[xml]$XmlDocument,
[string]$XPath,
[string]$Value
)

$node = $XmlDocument.SelectSingleNode($XPath)
if ($node) {
$node.InnerText = $Value
Write-Host "已更新节点:$XPath"
}
else {
Write-Host "未找到节点:$XPath"
}
}

# 使用示例
$configPath = "C:\App\config.xml"
$config = [xml](Get-Content $configPath)

Update-XMLNode -XmlDocument $config -XPath "//add[@key='ServerName']/@value" -Value "newserver"
Update-XMLNode -XmlDocument $config -XPath "//add[@key='Port']/@value" -Value "9090"

# 保存更改
$config.Save($configPath)

XML 数据验证:

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
# 创建 XML 验证函数
function Test-XMLValidation {
param(
[string]$XmlPath,
[string]$SchemaPath
)

try {
# 加载 XML 文档
$xml = New-Object System.Xml.XmlDocument
$xml.Load($XmlPath)

# 加载 XML Schema
$schema = New-Object System.Xml.Schema.XmlSchemaSet
$schema.Add("", $SchemaPath)

# 验证 XML
$xml.Schemas = $schema
$xml.Validate($null)

Write-Host "XML 验证成功"
return $true
}
catch {
Write-Host "XML 验证失败:$_"
return $false
}
}

XML 转换:

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
# 创建 XML 转换函数
function Convert-XMLToObject {
param(
[xml]$XmlDocument,
[string]$RootNode
)

$result = @()
$nodes = $XmlDocument.SelectNodes("//$RootNode")

foreach ($node in $nodes) {
$obj = [PSCustomObject]@{}
foreach ($child in $node.ChildNodes) {
$obj | Add-Member -NotePropertyName $child.Name -NotePropertyValue $child.InnerText
}
$result += $obj
}

return $result
}

# 使用示例
$xml = [xml]@"
<?xml version="1.0"?>
<users>
<user>
<name>张三</name>
<age>30</age>
<email>zhangsan@example.com</email>
</user>
<user>
<name>李四</name>
<age>25</age>
<email>lisi@example.com</email>
</user>
</users>
"@

$users = Convert-XMLToObject -XmlDocument $xml -RootNode "user"
$users | Format-Table -AutoSize

一些实用的 XML 处理技巧:

  1. XML 数据合并:

    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
    # 合并多个 XML 文件
    function Merge-XMLFiles {
    param(
    [string[]]$SourceFiles,
    [string]$OutputFile
    )

    $mergedXml = [xml]@"
    <?xml version="1.0" encoding="UTF-8"?>
    <mergedData>
    </mergedData>
    "@

    foreach ($file in $SourceFiles) {
    $xml = [xml](Get-Content $file)
    $root = $xml.DocumentElement

    # 导入节点
    $importedNode = $mergedXml.ImportNode($root, $true)
    $mergedXml.DocumentElement.AppendChild($importedNode)
    }

    # 保存合并后的文件
    $mergedXml.Save($OutputFile)
    Write-Host "已合并 XML 文件到:$OutputFile"
    }
  2. XML 数据过滤:

    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
    # 创建 XML 过滤函数
    function Filter-XMLData {
    param(
    [xml]$XmlDocument,
    [string]$XPath,
    [hashtable]$Conditions
    )

    $nodes = $XmlDocument.SelectNodes($XPath)
    $filteredNodes = @()

    foreach ($node in $nodes) {
    $match = $true
    foreach ($condition in $Conditions.GetEnumerator()) {
    $value = $node.SelectSingleNode($condition.Key).InnerText
    if ($value -ne $condition.Value) {
    $match = $false
    break
    }
    }

    if ($match) {
    $filteredNodes += $node
    }
    }

    return $filteredNodes
    }
  3. XML 数据导出:

    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
    # 导出数据为 XML
    function Export-ToXML {
    param(
    [object[]]$Data,
    [string]$RootNode,
    [string]$OutputFile
    )

    $xml = [xml]@"
    <?xml version="1.0" encoding="UTF-8"?>
    <$RootNode>
    </$RootNode>
    "@

    foreach ($item in $Data) {
    $node = $xml.CreateElement($item.GetType().Name)

    foreach ($prop in $item.PSObject.Properties) {
    $child = $xml.CreateElement($prop.Name)
    $child.InnerText = $prop.Value
    $node.AppendChild($child)
    }

    $xml.DocumentElement.AppendChild($node)
    }

    # 保存 XML 文件
    $xml.Save($OutputFile)
    Write-Host "已导出数据到:$OutputFile"
    }

这些技巧将帮助您更有效地处理 XML 数据。记住,在处理 XML 时,始终要注意数据的完整性和格式正确性。同时,建议在处理大型 XML 文件时使用流式处理方式,以提高性能。

PowerShell 技能连载 - XML 处理技巧

http://blog.vichamp.com/2024/10/18/powershell-xml-processing/

作者

吴波

发布于

2024-10-18

更新于

2025-03-25

许可协议

评论