我正在尝试使用标准编码/ xml包处理Go中具有复杂结构的XML文件,更改几个节点的值并保存交替的文件。例如:
while (myInput.hasNext()){
day = myInput.nextInt();
lowTemp = myInput.nextInt();
highTemp = myInput.nextInt();
if (highTemp >= 80)
hotD.println(day + " " + highTemp + " " + lowTemp);
else if (lowTemp < 30)
coldN.println(day + " " + highTemp + " " + lowTemp);
}
还有更多领域。我想更改节点:
<description>
<title-info>
<genre>Comedy</genre>
<author>
<first-name>Kevin</first-name>
<last-name>Smith</last-name>
</author>
<movie-title>Clerks</movie-title>
<annotation>
<p>!!!</p>
</annotation>
<keywords>comedy,jay,bob</keywords>
<date></date>
</description>
</title-info>
到
<author>
<first-name>Kevin</first-name>
<last-name>Smith</last-name>
</author>
但是,由于文件很大并且使用了超过50个标签,我真的不想描述解组它们的完整结构,所以我有
<author>
<first-name>K.</first-name>
<middle-name>Patrick</middle-name>
<last-name>Smith</last-name>
</author>
我需要使用的字段,但不知道如何保持文件的其余部分不受影响。看起来我需要使用xml.decode来选择我需要更改的节点(比如http://blog.davidsingleton.org/parsing-huge-xml-files-with-go/ post),同时跳过不需要的令牌到xml.encode,但是我无法将这个谜题转换为某些代码。
答案 0 :(得分:3)
您只使用标准库是一种约束吗?
如果没有,我建议etree(https://github.com/beevik/etree)将DOM放在标准库的XML处理之上。它有一个基本的xpath语法来选择节点,你可以轻松地编辑它们。