Golang XML处理
我试图使用standart encoding/XML包在Go中处理结构复杂的XML文件,更改两个节点的值并保存替换文件。例如:Golang XML处理,xml,go,encode,unmarshalling,Xml,Go,Encode,Unmarshalling,我试图使用standart encoding/XML包在Go中处理结构复杂的XML文件,更改两个节点的值并保存替换文件。例如: <description> <title-info> <genre>Comedy</genre> <author> <first-name>Kevin</first-name> <last-na
<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>
对于需要处理的字段,但不知道如何保持文件的其余部分不变。看起来我需要使用xml.decode来选择需要更改的节点(如在post中),同时将不需要的标记跳过xml.encode,但我无法将此难题转换为一些代码 仅使用标准库是否是一种限制
如果不是,我建议使用etree(),它将DOM置于标准库的XML处理之上。它有一个基本的xpath语法来选择节点,一旦有了它们,您就可以轻松地编辑它们。我想您可以试试XSLT。可能是?不幸的是,这引入了对
libxml2
的依赖。
<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>
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"`
}