如何使用PowerShell在XML文件中的CDATA中获取值? [英] How to get values in CDATA in XML file with PowerShell?
问题描述
我对正在访问的XML中的CDATA字符串感到困惑。
I have a puzzling situation with a CDATA string from XML I am accessing.
我正在访问XML API,并在PowerShell中收集CDATA字符串。它返回如下:
I am access an XML API and collecting the CDATA string in PowerShell. 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
您可以看到有多行。我想将其收集到变量中,例如 $ aggressorAllianceID = 99006227
。
As you can see there are multiple lines. I want to collect this to variables so for example $aggressorAllianceID = 99006227
.
完整的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>
我试图用split命令来解析CDATA字符串,但我真的不知所措吗?
I have tryed to parse the CDATA string with the split command but I'm not really getting awhere?
任何人都可以给我提供一个示例,说明他们如何访问和更改此CDATA信息吗?
Can anyone provide me an example of how they would access and variable this CDATA information?
推荐答案
存储数据的规范方法是哈希表。使用 ConvertFrom-StringData
cmdlet用于将字符串中的键=值对列表转换为哈希表数据结构。由于您的数据包含冒号而不是 =
,因此您需要先替换它们。
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. 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']
...
这篇关于如何使用PowerShell在XML文件中的CDATA中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!