XML Golang Processing

I am trying to process XML files with complex structure in Go using the standard encoding / xml package, change the values โ€‹โ€‹of a node pair, and save an alternate 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 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 the files are massive and use 50+ tags, I really don't want to describe the complete structure to untie 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 that I need to work with but don't know how to keep the rest of the file intact. It looks like I need to use xml.decode to select the nodes that I need to change (e.g. http://blog.davidsingleton.org/parsing-huge-xml-files-with-go/ post), skipping unneeded tokens in the xml. encode, but I can't seem to convert this puzzle to some kind of code.

+3


source to share


1 answer


Is it a limitation that you are only using the standard library?



If not, I would recommend etree ( https://github.com/beevik/etree ), which puts the DOM on top of the standard XML library processing. It has basic xpath syntax for selecting nodes and you can easily edit them once they are there.

+3


source







All Articles