我有一个大约有10,000行的XML文件,我想将一些数据提取到CSV文件中。
我遇到的问题是提取id
属性和联盟名称标签的内部文本。如果有人能够阐明为什么ID没有显示以及如何获取innertext那将是非常有帮助的。
重复样本数据
<players>
<towns>
<town>
<player>
<playername id="1">MyName</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="2">Tonka</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="2">Tonka</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="2">Tonka</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="2">Tonka</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="2">Tonka</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="2">Tonka</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="2">Tonka</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="2">Tonka</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="2">Tonka</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="18">DaVS</alliancename>
<alliancetag>DaVS</alliancetag>
<alliancetax>0.01</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="3">Sinner</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="1">Harmless?</alliancename>
<alliancetag>TH</alliancetag>
<alliancetax>0.00</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="3">Sinner</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="1">Harmless?</alliancename>
<alliancetag>TH</alliancetag>
<alliancetax>0.00</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="3">Sinner</playername>
<playerrace>Human</playerrace>
<playeralliance>
<alliancename id="1">Harmless?</alliancename>
<alliancetag>TH</alliancetag>
<alliancetax>0.00</alliancetax>
</playeralliance>
</player>
</town>
<town>
<player>
<playername id="10">Kumomoto</playername>
<playerrace>Elf</playerrace>
<playeralliance>
<alliancename id="1">Harmless?</alliancename>
<alliancetag>TH</alliancetag>
<alliancetax>0.00</alliancetax>
</playeralliance>
</player>
</town>
</towns>
</players>
当前代码
$xml.selectNodes('//playeralliance') |
select alliancename.id, allianceticker, alliancetaxrate
输出
PS C:\Users\jon> $xml.selectNodes('//playeralliance') | select alliancename.id, alliancename, alliancetag, alliancetax
alliancename.id alliancename alliancetag alliancetax
--------------- ------------ ----------- -----------
alliancename DaVS 0.01
alliancename DaVS 0.01
alliancename DaVS 0.01
alliancename DaVS 0.01
alliancename DaVS 0.01
alliancename DaVS 0.01
alliancename DaVS 0.01
alliancename DaVS 0.01
alliancename DaVS 0.01
alliancename DaVS 0.01
alliancename TH 0.00
alliancename TH 0.00
alliancename TH 0.00
alliancename TH 0.00
过去的代码示例
[xml]$xml = Get-Content 'C:\Users\jhold\Desktop\Illyriad Data\sample.xml';
$xml.towns.town.player;
foreach($item in $xml.playeralliance)
{
$allianceName = $item
#$allianceID = $item.id
#$allianceTicker = $item.allianceticker
#$allianceTax = $item.alliancetax
Write-host "Name: " $allianceName
#Write-host "ID: " $allianceID
#Write-host "Ticker: " $allianceTicker
#Write-host "Tax: " $allianceTax
}
答案 0 :(得分:4)
$xml.SelectNodes('//playeralliance') |
Select-Object @{n='allianceid';e={$_.alliancename.id}},
@{n='alliancename';e={$_.alliancename.'#text'}},
@{n='allianceticker';e={$_.alliancetag}},
@{n='alliancetaxrate';e={$_.alliancetax}}
答案 1 :(得分:1)
如果我没记错的话,那就是powershell v3 +
PS C:\Users\joshua> [xml]$xml = Get-Content C:\Users\joshua\Desktop\sample.xml
$xml.town.player
$xml.town.player.playername.id
$xml.town.player.playername.'#text'
playername playerrace playeralliance
---------- ---------- --------------
playername Elf playeralliance
10
MyName
PS C:\Users\joshua>
答案 2 :(得分:1)
利用扩展属性......它对我有用。
router.get('/hello', (req, res) => {
res.send('This get executed');
});
router.get('/hello', (req, res) => {
res.send('This will NOT get executed since you are ending the response with res.send in the route above');
});