使用 PowerShell 解析 XML:获取具有属性的元素的值
Parsing XML with PowerShell: Getting the value of an element with an attribute
我有一个 XML 文件,其中包含带属性的元素。我希望能够读取属性值和元素值。虽然我可以获得属性值,但当我尝试读取元素值时,它只是 returns 标签名称,而不是值。
XML 文件 C:\Temp\books2.xml:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date inprint="false">2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date inprint="true">2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date inprint="false">2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>
我尝试过的:
$xmlDoc = new-object -TypeName xml
$filePath = "C:\Temp\Books2.xml"
$xmlDoc.Load($filePath)
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date}, {$_.publish_date.inprint}
结果:
author : Gambardella, Matthew
title : XML Developer's Guide
publish_date : publish_date
$_.publish_date : publish_date
$_.publish_date.inprint : false
author : Ralls, Kim
title : Midnight Rain
publish_date : publish_date
$_.publish_date : publish_date
$_.publish_date.inprint : true
author : Corets, Eva
title : Maeve Ascendant
publish_date : publish_date
$_.publish_date : publish_date
$_.publish_date.inprint : false
如何读取还包含属性(在本例中为 publish_date)的元素的值?我想读取 publish_date 的日期值,而不是标签名称。
您正在尝试访问 publish_date
XML 元素的文本内容。
为此,请使用以下语法:
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date.'#text'}, {$_.publish_date.inprint}
我检查了 PowerShell 返回的对象的数据类型:($xmlDoc.catalog).gettype().fullname
。结果表明,代表节点的对象具有数据类型 System.Xml.XmlElement。所以我尝试使用 XmlElement.InnerText 属性,并且有效:
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date.innertext}, {$_.publish_date.inprint} | format-list
结果:
author : Gambardella, Matthew
title : XML Developer's Guide
publish_date : publish_date
$_.publish_date.innertext : 2000-10-01
$_.publish_date.inprint : false
author : Ralls, Kim
title : Midnight Rain
publish_date : publish_date
$_.publish_date.innertext : 2000-12-16
$_.publish_date.inprint : true
author : Corets, Eva
title : Maeve Ascendant
publish_date : publish_date
$_.publish_date.innertext : 2000-11-17
$_.publish_date.inprint : false
我有一个 XML 文件,其中包含带属性的元素。我希望能够读取属性值和元素值。虽然我可以获得属性值,但当我尝试读取元素值时,它只是 returns 标签名称,而不是值。
XML 文件 C:\Temp\books2.xml:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date inprint="false">2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date inprint="true">2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date inprint="false">2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>
我尝试过的:
$xmlDoc = new-object -TypeName xml
$filePath = "C:\Temp\Books2.xml"
$xmlDoc.Load($filePath)
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date}, {$_.publish_date.inprint}
结果:
author : Gambardella, Matthew
title : XML Developer's Guide
publish_date : publish_date
$_.publish_date : publish_date
$_.publish_date.inprint : false
author : Ralls, Kim
title : Midnight Rain
publish_date : publish_date
$_.publish_date : publish_date
$_.publish_date.inprint : true
author : Corets, Eva
title : Maeve Ascendant
publish_date : publish_date
$_.publish_date : publish_date
$_.publish_date.inprint : false
如何读取还包含属性(在本例中为 publish_date)的元素的值?我想读取 publish_date 的日期值,而不是标签名称。
您正在尝试访问 publish_date
XML 元素的文本内容。
为此,请使用以下语法:
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date.'#text'}, {$_.publish_date.inprint}
我检查了 PowerShell 返回的对象的数据类型:($xmlDoc.catalog).gettype().fullname
。结果表明,代表节点的对象具有数据类型 System.Xml.XmlElement。所以我尝试使用 XmlElement.InnerText 属性,并且有效:
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date.innertext}, {$_.publish_date.inprint} | format-list
结果:
author : Gambardella, Matthew
title : XML Developer's Guide
publish_date : publish_date
$_.publish_date.innertext : 2000-10-01
$_.publish_date.inprint : false
author : Ralls, Kim
title : Midnight Rain
publish_date : publish_date
$_.publish_date.innertext : 2000-12-16
$_.publish_date.inprint : true
author : Corets, Eva
title : Maeve Ascendant
publish_date : publish_date
$_.publish_date.innertext : 2000-11-17
$_.publish_date.inprint : false