【发布时间】:2020-11-18 17:01:42
【问题描述】:
我有一个非常大的文本文件 (209 MB),其中包含我试图隔离的一大块 XML。文本文件是两个不同的 XML 段,我想要第二段。使用 PowerShell,我正在尝试使用 match 从我想要的开头隔离到结尾,寻找一个始终位于我想要的段的开头和结尾的特定字符串,它不起作用:
$Hello = Get-Content "E:\sandbox\test.txt"
$Hello -match "</ns1:GetAllCompliancesResponse>(?<content>.*)</soapenv:Body>"
$Hello = $Matches['content']
$Hello | Out-File -FilePath "E:\sandbox\testoutput.txt"
当我在 PS 中运行它时出现空数组错误。我不太确定问题出在哪里。
在我得到很大帮助后添加更多内容:
我的数据源是一个 200mb 的文件。由于它的大小,测试解析是一个艰巨的过程。如果我的一般结构是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:GetAllCompliancesResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://webservices.web.arber.arb.ca.gov">
<GetAllCompliancesReturn soapenc:arrayType="ns2:ComplianceSummary[263026]" xsi:type="soapenc:Array" xmlns:ns2="urn:DrayageTruckStatusService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<GetAllCompliancesReturn href="#id0"/>
<GetAllCompliancesReturn href="#id1"/>
<GetAllCompliancesReturn href="#id2"/>
<GetAllCompliancesReturn href="#id3"/>
<GetAllCompliancesReturn href="#id4"/>
<GetAllCompliancesReturn href="#id263024"/>
<GetAllCompliancesReturn href="#id263025"/>
</GetAllCompliancesReturn>
</ns1:GetAllCompliancesResponse>
<multiRef id="id83299" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:ComplianceSummary" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns3="urn:TruckStatusService">
<dtrNumber xsi:type="xsd:string">*********</dtrNumber>
<licensePlateNumber xsi:type="xsd:string">*******</licensePlateNumber>
<licensePlateState xsi:type="xsd:string">**</licensePlateState>
<status xsi:type="xsd:string">************</status>
<traceNumber xsi:type="xsd:int" xsi:nil="true"/>
<untilDate xsi:type="xsd:date">***********</untilDate>
<vin xsi:type="xsd:string">**********************</vin>
</multiRef>
<multiRef id="id132635" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns4:ComplianceSummary" xmlns:ns4="urn:TruckStatusService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<dtrNumber xsi:type="xsd:string">*********</dtrNumber>
<licensePlateNumber xsi:type="xsd:string">*******</licensePlateNumber>
<licensePlateState xsi:type="xsd:string">**</licensePlateState>
<status xsi:type="xsd:string">***********</status>
<traceNumber xsi:type="xsd:int" xsi:nil="true"/>
<untilDate xsi:type="xsd:date">***********</untilDate>
<vin xsi:type="xsd:string">**********************</vin>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
如何形成 XPath 以获取 multiRef 节点中的项目?
【问题讨论】:
标签: powershell