I am trying to process XML files with complicated structure in Go using the standart encoding/xml package, change values of couple of nodes and save the alternated file. For example:
<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>
And many more fields. I would like to change the node:
<author>
<first-name>Kevin</first-name>
<last-name>Smith</last-name>
</author>
to
<author>
<first-name>K.</first-name>
<middle-name>Patrick</middle-name>
<last-name>Smith</last-name>
</author>
However, since files are massive and uses more then 50 tags I really don't want to describe the complete structure to unmarshal them, so I have
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"`
}
for fields I need to work with, but don't know how to keep rest of the file untouched. Looks like I need to use xml.decode to select the nodes I need to change (like at http://blog.davidsingleton.org/parsing-huge-xml-files-with-go/ post) while skipping unneeded tokens to xml.encode, but I can't convert this puzzle to some code.
Is it a constraint that you only use the standard library?
If not, I'd recommend etree (https://github.com/beevik/etree) which puts a DOM on top of the standard library's XML processing. It has a basic xpath syntax to select nodes, and you can easily edit them once you have them.