I often found myself wanting to generate or process an XML document without having to rely on marshaling or encoding/decoding logic.
I am trying to process XML files with complicated structure with Go, change values of couple of nodes and save to the alternated file while preserving the rest. 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 effect_range="1999-2011">Sam</first-name>
<first-name effect_range="2012-">Kevin</first-name>
<last-name>Smith</last-name> <full-name></full-name>
</author>to
<author>
<first-name effect_range="1999-2011">Sam</first-name>
<first-name effect_range="2012-">Kevin</first-name>
<last-name>Smith</last-name>
<full-name>Kevin Smith</full-name>
</author>I.e., I want to manipulation one xml node according to some previous xml nodes met earlier or, from node attributes somewhere above. However, since files have massive tags I really don't want to describe the complete structure.
I know etree (https://github.com/beevik/etree) can put a DOM on top of the standard library's XML processing, and it has a basic xpath syntax to select nodes, however, is it possible for me to use etree to,
Why not just iterate over the tokens, echo what you don't want to change, store what you need, and echo a modified token when reach that specific tag?
I thoight that std encoding/xml.Decoder.Token() would work, calling it in a loop.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/tf4aDQ1Hn_c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
It's rough at a lot of places, but from here, adding namespace handling, and the filtering logic would be easy.
If the XMLName field has an associated tag of the form "name" or "namespace-URL name", the XML element must have the given name...
Adding the namespace handling seems to be a challenging part to me. I looked through my http://localhost:6060/pkg/encoding/xml/, and found only one spot mentioning "namespace",
On Nov 28, 2015, at 1:12 PM, Tong Sun wrote:Adding the namespace handling seems to be a challenging part to me. I looked through my http://localhost:6060/pkg/encoding/xml/, and found only one spot mentioning "namespace",There are other more powerful packages, but I wrote aqwari.net/xml/xmltree, which may do what you need. I wrote it for the purpose of writing a set of packages for working with xml schema, WSDL, and soap. Specifically, it handles namespaces “correctly”, to the best of my understanding of the xml spec.
One thing that was difficult just with encoding/xml was decoding qnames in attribute values; e.g. the “type” attribute in<myelem type=“ns1:int”>3</myelem>This does not seem to be too important for your use case,
this should do what you need; See http://play.golang.org/p/Cb1fvUxieE . For more examples, see the documentation for Search and SearchFunc: https://godoc.org/aqwari.net/xml/xmltree#Element.SearchFunc
The Unmarshal method unmarshals an XML fragment as it was returned by the Parse method; further modifications to a tree of Elements are ignored by the Unmarshal method.