
XML文件是有一定格式要求的文本文件。
百度百科
可扩展标记语言 (Extensible Markup Language, XML) , 标准通用标记语言 的子集,可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。 XML是标准通用标记语言 可扩展性良好,内容与形式分离,遵循严格的语法要求,保值性良好等优点。
参考文档
基本特点:
XML是一个树形数据结构
XML有且只有一个根节点
XML有0个或多个子节点
把XML看作对象,则XML对象具有根节点属性,同时根节点有0个或多个子节点属性
每个节点一定包含节点名称,节点中可以包含属性也可以不包含属性
如下Delphi工程中的.dproj部分XML文件结构(VCL_DEMO.dproj):
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{A524FB1B-A9A8-4FB6-A680-1D05B7B5E1BF}</ProjectGuid>
<ProjectVersion>19.5</ProjectVersion>
<FrameworkType>VCL</FrameworkType>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
<Platform Condition="'$(Platform)'==''">Win64</Platform>
<TargetedPlatforms>3</TargetedPlatforms>
<AppType>Application</AppType>
<MainSource>VCL_DEMO.dpr</MainSource>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
<Base>true</Base>
</PropertyGroup>
<Project>
PowerShell 解析操作XML文件,使用XML对象:
# 定义xml对象
$xml = New-Object -TypeName xml
# 查看 $xml 对象的所有方法和属性
Get-Member -InputObject $xml
关于$xml对象包含的操作方法如下:
TypeName:System.Xml.XmlDocument
Name MemberType
---- ----------
ToString CodeMethod
AppendChild Method
Clone Method
CloneNode Method
CreateAttribute Method
CreateCDataSection Method
CreateComment Method
CreateDocumentFragment Method
CreateDocumentType Method
CreateElement Method
CreateEntityReference Method
CreateNavigator Method
CreateNode Method
CreateProcessingInstruction Method
CreateSignificantWhitespace Method
CreateTextNode Method
CreateWhitespace Method
CreateXmlDeclaration Method
Equals Method
GetElementById Method
GetElementsByTagName Method
GetEnumerator Method
GetHashCode Method
GetNamespaceOfPrefix Method
GetPrefixOfNamespace Method
GetType Method
ImportNode Method
InsertAfter Method
InsertBefore Method
Load Method
LoadXml Method
Normalize Method
PrependChild Method
ReadNode Method
RemoveAll Method
RemoveChild Method
ReplaceChild Method
Save Method
SelectNodes Method
SelectSingleNode Method
Supports Method
Validate Method
WriteContentTo Method
WriteTo Method
Item ParameterizedProperty
每个方法的具体使用可以查询 System.Xml.XmlDocument 的相关资料。
#! PowerShell 操作XML
<#
@ parse_xml.ps1
@ 解析 xml
@ by sensor
#>
# 输入/输出
$input_xmlfile = Join-Path $PSScriptRoot 'VCL_DEMO.dproj'
# 定义xml对象
$xml = New-Object -TypeName xml
try {
# 调用xml的load方法导入xml文件,若输入为字符串使用loadxml
$xml.Load($input_xmlfile)
#获取根节点
$root=$xml.FirstChild
if ($null -ne $root) { # 判断是否存在根节点Project
$Project_xmlns = if ($null -ne $root.xmlns) {$root.xmlns} #获取xmlns属性
if ($null -ne $Project_xmlns) { # 判断是否存在子节点PropertyGroup
Write-Output "Prject的xmlns属性: $Project_xmlns"
#获取根节点的所有子节点列表
$PropertyGroup = $root.ChildNodes
# 遍历PropertyGroup节点的所有子节点,获取相应属性
$PropertyGroup | ForEach-Object {
# 查找一个没有属性的节点
$value = $_.HasAttributes
if ($value -eq $False){ # 没有任何属性的节点
$_.ChildNodes | ForEach-Object{
# 显示出来
$_.name + ' ' + $_.innerText
# 修改 FrameworkType 节点的值为 FMX
if($_.name -eq 'FrameworkType'){$_.innerText = 'FMX'}
}
}
}
}
}
# 保存修改
$xml.Save($input_xmlfile)
}
catch {
# 如果输入文件$input_file非xml格式则无法转换为xml
throw "无法解析 ${input_file} 为XML 文件"
}
运行后,会将FrameworkType子节点更改为FMX

以上演示了PowerShell 操作XML文件方法,最主要的是需要学会查询文档
今天是大年初三,再祝大家新年快乐,开心如意!