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

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

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/

相关文章:

java - `Thread.sleep` 与 Project Loom for Java 中的虚拟

r - 有没有办法根据名称自动执行 R 中列的乘法?

google-apps-script - 无法读取 null 的属性 'getSheetByName

reactjs - 类型 'onClick' 上不存在属性 '{ children?: ReactN

bash - 删除重复行立即忽略第一个字段

list - 如何将列表合并到 Elixir 中的元组列表中?

linux-kernel - linux cdc_ecm 驱动程序与 rndis 驱动程序

python - 从 Pandas 数据框中同一字段的所有其他行中减去一行字段中的值

python - 如何在输出文件中返回输入文件的第 k 个元素?

r - as.integer(8952) = 8951?