AHK将多个xml元素按顺序解析为[.txt]文件

问题描述 投票:2回答:1

我已经到了可以获取我想要的元素值的地方,并将它们附加到txt文件。我遇到的问题是顺序追加它们。我的xml文件中的摘录/示例是:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Arcade>
  <Game>
    <Title>t1</Title>
    <Publisher>p1</Publisher>
    <Source>s1</Source>
    <Version>v1</Version>
    <Genre>g1</Genre>
  </Game>
  <Game>
    <Title>t2</Title>
    <Publisher>p2</Publisher>
    <Source>s2</Source>
    <Version>v2</Version>
    <Genre>g2</Genre>
  </Game>
  <Game>
    <Title>t3</Title>
    <Publisher>p3</Publisher>
    <Source>s3</Source>
    <Version>v3</Version>
    <Genre>g3</Genre>
  </Game>
</Arcade>

我希望看到的输出是:

t1 s1 g1
t2 s2 g2
t3 s3 g3

我的基线脚本:

#NoEnv  
SendMode Input  
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

xmlPath := "D:\temp\Arcade.xml"
xmlDoc := ComObjCreate("MSXML2.DOMDocument.6.0")
xmlDoc.async := false
xmlDoc.load(xmlPath)

Loop Files, %xmlPath%
{
    for item in xmlDoc.getElementsByTagName("Title") {
        Tstring := item.text
        FileAppend, %Tstring% , D:\temp\testoutput.txt
        }
    for item in xmlDoc.getElementsByTagName("Source") {
        Sstring := item.text
        FileAppend, %Sstring% , D:\temp\testoutput.txt
        }
    for item in xmlDoc.getElementsByTagName("Genre") {
        Gstring := item.text
        FileAppend, %Gstring%`n, D:\temp\testoutput.txt
        }
ExitApp
}

结果如下:

t1 t2 t3 s1 s2 s3
g1
g2
g3

我试过移动关闭的'花括号'和FileAppend类似于:

Loop Files, %xmlPath%
{
    for item in xmlDoc.getElementsByTagName("Title") {
        Tstring := item.text
    for item in xmlDoc.getElementsByTagName("Source") {
        Sstring := item.text
    for item in xmlDoc.getElementsByTagName("Genre") {
        Gstring := item.text
        FileAppend, %Tstring%|%Sstring%|%Gstring%`n, D:\temp\testoutput.txt
        }
        }
        }
ExitApp
}

..它给了我:

t1 s1 g1
t1 s1 g2
t1 s1 g3
t1 s2 g1
t1 s2 g2
t1 s2 g3
t1 s3 g1
t1 s3 g2
t1 s3 g3
t2 s1 g1
t2 s1 g2
t2 s1 g3
...

还有一些其他的迭代。我知道(或者至少觉得)我在正确的轨道上,如果这是MasterMind游戏,我想我现在可能已经拥有它。 :)唉,事实并非如此。

任何帮助和指导将不胜感激。

xml-parsing autohotkey
1个回答
2
投票

最简单的事情可能是逐个游戏,例如:

for Game in xmlDoc.getElementsByTagName("Game") {
    Text := ""
    Text .= Game.getElementsByTagName("Title").item(0).text
    Text .= Game.getElementsByTagName("Source").item(0).text
    Text .= Game.getElementsByTagName("Genre").item(0).text
    MsgBox % Text
}
© www.soinside.com 2019 - 2024. All rights reserved.