简体   繁体   English

如何使用PowerShell在XML文件中的CDATA中获取值?

[英]How to get values in CDATA in XML file with PowerShell?

I have a puzzling situation with a CDATA string from XML I am accessing. 我正在访问的XML中的CDATA字符串令人困惑。

I am access an XML API and collecting the CDATA string in PowerShell. 我正在访问XML API,并在PowerShell中收集CDATA字符串。 It returns like this: 它返回如下:

aggressorAllianceID: 99006227
aggressorCorpID: 244898283
aggressorID: 1283793762
armorValue: 1.0
hullValue: 1.0
moonID: 40043321
shieldValue: 0.5166110755741394
solarSystemID: 30000686
typeID: 20060

As you can see there are multiple lines. 如您所见,有多行。 I want to collect this to variables so for example $aggressorAllianceID = 99006227 . 我想将其收集到变量中,例如$aggressorAllianceID = 99006227

The full XML is here: 完整的XML在这里:

<eveapi version="2">
<currentTime>2017-07-19 09:08:18</currentTime>
<result>
<rowset name="notifications" key="notificationID" columns="notificationID">
<row notificationID="644139237">
<![CDATA[
aggressorAllianceID: 99006227 aggressorCorpID: 244898283 aggressorID: 1283793762 armorValue: 1.0 hullValue: 1.0 moonID: 40043321 shieldValue: 0.5166110755741394 solarSystemID: 30000686 typeID: 20060
]]>
</row>
</rowset>
</result>
<cachedUntil>2027-07-17 09:08:18</cachedUntil>
</eveapi>

I have tryed to parse the CDATA string with the split command but I'm not really getting awhere? 我试图用split命令来解析CDATA字符串,但是我真的不明白吗?

Can anyone provide me an example of how they would access and variable this CDATA information? 谁能给我一个例子,说明他们将如何访问和更改此CDATA信息?

The canonical way of storing data like that is a hashtable . 像这样存储数据的规范方法是哈希表 Use the ConvertFrom-StringData cmdlet for transforming a list of key=value pairs in a string to a hashtable data structure. 使用ConvertFrom-StringData cmdlet将字符串中的键=值对列表转换为哈希表数据结构。 Since your data has colons instead of = you need to replace them first. 由于您的数据包含冒号而不是=您需要先替换它们。

$cdata = @'
aggressorAllianceID: 99006227
aggressorCorpID: 244898283
aggressorID: 1283793762
armorValue: 1.0
hullValue: 1.0
moonID: 40043321
shieldValue: 0.5166110755741394
solarSystemID: 30000686
typeID: 20060
'@

$ht = $cdata -replace ':', '=' | ConvertFrom-StringData

Then you can access the individual values like this: 然后,您可以像这样访问各个值:

$ht['aggressorID']
$ht['hullValue']
...

Undeleted due to Tomalak's helpful comments - please read before considering New-Variable 由于Tomalak的有用评论而未删除-在考虑使用New-Variable之前请先阅读

Ansgar's answer is a better way to store this data and deal with this programmatically. Ansgar的答案是一种更好的方式来存储数据并以编程方式进行处理。 To answer your literal question of how to get, for example $aggressorAllianceID = 99006227 , you can use New-Variable 要回答有关如何获取的字面问题,例如$aggressorAllianceID = 99006227 ,可以使用New-Variable

input.txt input.txt中

aggressorAllianceID: 99006227
aggressorCorpID: 244898283
aggressorID: 1283793762
armorValue: 1.0
hullValue: 1.0
moonID: 40043321
shieldValue: 0.5166110755741394
solarSystemID: 30000686
typeID: 20060

$myReturnedValues = Get-content .\input.txt
foreach($line in $myReturnedValues){
    New-Variable -Name ($line.Split(": "))[0] -Value ($line.Split(": "))[2]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM