gpt4 book ai didi

xml - 如何使用 ConvertTo-Xml 和 Select-Xml 加载或读取 XML 文件?

转载 作者:行者123 更新时间:2023-12-05 08:37:34 26 4
gpt4 key购买 nike

我怎样才能完成这样的事情:

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $date=(Get-Date | ConvertTo-Xml)
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $date

xml Objects
--- -------
version="1.0" encoding="utf-8" Objects

PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $date.OuterXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.DateTime">12/12/2020 2:43:46 AM</Object></Objects>
PS /home/nicholas/powershell>

而是读取文件?


如何使用 ConvertTo-Xml 加载/导入/读取/转换 xml 文件以使用 Select-Xml 进行解析>Xpath?

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $xml=ConvertTo-Xml ./bookstore.xml
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml

xml Objects
--- -------
version="1.0" encoding="utf-8" Objects

PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml.InnerXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.String">./bookstore.xml</Object></Objects>
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml.OuterXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.String">./bookstore.xml</Object></Objects>
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> cat ./bookstore.xml

<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>

PS /home/nicholas/powershell>

REPL 控制台中创建 xml 文件本身按预期工作:

How to parse XML in Powershell with Select-Xml and Xpath?

最佳答案

正确地在 Powershell 中读取 XML 文档的工作方式如下:

$doc = New-Object xml
$doc.Load( (Convert-Path bookstore.xml) )

XML 可以采用多种文件编码,并使用 the XmlDocument.Load method确保在事先不知道编码的情况下正确读取文件。

除非在非常基本或非常幸运的情况下,否则不使用正确的编码读取文件将导致数据损坏或错误。

常见的使用方法Get-Content并将结果字符串转换为 [xml]正是出于这个原因,这是处理 XML 的错误方法。所以不要那样做。

您可以使用 Get-Content 得到正确的结果, 但这需要

  1. 文件编码的先验知识(例如 Get-Content bookstore.xml -Encoding UTF8)
  2. 将文件编码硬编码到您的脚本中(这意味着如果 XML 编码曾经意外更改,它将中断)
  3. 将自己限制在极少数的文件编码中 Get-Content支持(XML支持更多)

这意味着您将自己置于必须手动思考和解决 XML 专门设计用于自动为您处理的问题的位置。

使用 Get-Content 正确地做事是很多不必要的额外工作和限制。错误地做事毫无意义,因为正确地做事很容易。


示例,加载后$doc如上所示。

$doc.bookstore.book

打印 <book> 的列表元素及其属性

genre           : novel
publicationdate : 1997
ISBN : 1-861001-57-8
title : Pride And Prejudice
author : author
price : 24.95

genre : novel
publicationdate : 1992
ISBN : 1-861002-30-1
title : The Handmaid's Tale
author : author
price : 29.95

genre : novel
publicationdate : 1991
ISBN : 1-861001-57-6
title : Emma
author : author
price : 19.95

genre : novel
publicationdate : 1982
ISBN : 1-861001-45-3
title : Sense and Sensibility
author : author
price : 19.95

$doc.bookstore.book | Format-Table

打印和表格一样的东西

genre publicationdate ISBN          title                 author price
----- --------------- ---- ----- ------ -----
novel 1997 1-861001-57-8 Pride And Prejudice author 24.95
novel 1992 1-861002-30-1 The Handmaid's Tale author 29.95
novel 1991 1-861001-57-6 Emma author 19.95
novel 1982 1-861001-45-3 Sense and Sensibility author 19.95

$doc.bookstore.book | Where-Object publicationdate -lt 1992 | Format-Table

过滤数据

genre publicationdate ISBN          title                 author price
----- --------------- ---- ----- ------ -----
novel 1991 1-861001-57-6 Emma author 19.95
novel 1982 1-861001-45-3 Sense and Sensibility author 19.95

$doc.bookstore.book | Where-Object publicationdate -lt 1992 | Sort publicationdate | select title

仅排序并打印 <title>字段

title                
-----
Sense and Sensibility
Emma

对数据进行切片和切 block 的方法还有很多,这完全取决于你想做什么。

关于xml - 如何使用 ConvertTo-Xml 和 Select-Xml 加载或读取 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65263942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com