dimanche 28 juin 2015

Golang XML processing

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. Looks like I need to use xml.decode to select the nodes I need to change while skipping unneeded tokens to xml.encode, but I can't convert this puzzle to some code.

Aucun commentaire:

Enregistrer un commentaire