gpt4 book ai didi

xml-parsing - AHK 将多个xml元素依次解析成[.txt]文件

转载 作者:行者123 更新时间:2023-12-05 05:11:53 24 4
gpt4 key购买 nike

我已经到了可以获取所需元素值并将它们附加到 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 游戏,我想我现在可能已经拥有了。 :) 唉,不是。

如有任何帮助和指导,我们将不胜感激。

最佳答案

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

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
}

关于xml-parsing - AHK 将多个xml元素依次解析成[.txt]文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54989312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com