我试图在Go中使用standart编码/ XML包处理结构复杂的xml文件,更改几个节点的值,并保存备用文件。例如:
<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>至
<author>
<first-name>K.</first-name>
<middle-name>Patrick</middle-name>
<last-name>Smith</last-name>
</author>然而,由于文件很庞大,并且使用了超过50个标签,我真的不想描述完整的结构来对它们进行解组,所以我有
type Result struct {
Title string `xml:"description>title-info>movie-title"`
Authors []Author `xml:"description>title-info>author"`
}
type Author struct {
Fname string `xml:"first-name"`
Mname string `xml:"middle-name"`
Lname string `xml:"last-name"`
}对于我需要处理的字段,但不知道如何保持文件的其余部分不受影响。看起来我需要使用xml.decode来选择我需要更改的节点(就像在http://blog.davidsingleton.org/parsing-huge-xml-files-with-go/ post中一样),同时跳过不需要的令牌到xml.encode,但是我不能将这个难题转换成一些代码。
发布于 2015-07-31 06:41:33
只使用标准库是一种限制吗?
如果没有,我推荐使用etree (https://github.com/beevik/etree),它将DOM放在标准库的https://github.com/beevik/etree处理之上。它有一个基本的xpath语法来选择节点,一旦有了节点,就可以很容易地对它们进行编辑。
https://stackoverflow.com/questions/31104777
复制相似问题