modify xml by xpath

721 views
Skip to first unread message

gauravk

unread,
Feb 3, 2015, 10:05:08 PM2/3/15
to golan...@googlegroups.com
Hello Gophers,

I am trying to convert a java application to Go. The basic workflow is as:

1. Read an XML file in DOM document
2. Modify nodes / attributes by finding them with Xpath
3. Convert back DOM document in XML stream / string
4. Send XML over the http. 

So I checked following approaches:

I checked 'encoding/xml' package It seems to me that I need to fully define structs for XML document to unmarshal and modify a particular node or attribute. 

OR

Using 'gopkg.in/xmlpath.v2' I can capture node by xpath and modify.

First approach would work fine but there are like ~20 types of atrociously designed XML files. So it will take long time to write structs for all to unmarshal

In second approach I could not find way to convert *xmlpath.Node back XML stream / string.

Any insight on how to solve this problem in Go is appreciated.

Thanks

Egon

unread,
Feb 4, 2015, 1:50:08 AM2/4/15
to golan...@googlegroups.com


On Wednesday, 4 February 2015 05:05:08 UTC+2, gauravk wrote:
Hello Gophers,

I am trying to convert a java application to Go. The basic workflow is as:

1. Read an XML file in DOM document
2. Modify nodes / attributes by finding them with Xpath
3. Convert back DOM document in XML stream / string
4. Send XML over the http. 

So I checked following approaches:

I checked 'encoding/xml' package It seems to me that I need to fully define structs for XML document to unmarshal and modify a particular node or attribute. 

OR

Using 'gopkg.in/xmlpath.v2' I can capture node by xpath and modify.

First approach would work fine but there are like ~20 types of atrociously designed XML files. So it will take long time to write structs for all to unmarshal

There is https://github.com/metaleap/go-xsd that can generate go structs based on xsd specification - I haven't used it myself so I don't know how good it is.
 

In second approach I could not find way to convert *xmlpath.Node back XML stream / string.

It shouldn't be too difficult to add that serialization to: https://github.com/go-xmlpath/xmlpath/tree/v2

(Unless it already does that with either String() or Bytes() method)

C Banning

unread,
Feb 4, 2015, 6:04:52 AM2/4/15
to golan...@googlegroups.com

roger peppe

unread,
Feb 4, 2015, 6:44:47 AM2/4/15
to gauravk, golang-nuts
Be aware that currently the encoding/xml package is broken with
respect to marshaling XML with namespaces. I'm hoping
that the changes in https://go-review.googlesource.com/#/c/2660/4
will make it into 1.5, which should fix that.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

gauravk

unread,
Feb 4, 2015, 9:01:35 AM2/4/15
to golan...@googlegroups.com
I will try for serialization or end up writing some structs as and when needed. String() returns data in xml.
'xsd' package you mentioned would be greatly useful. Alas, nice folks here prefer designing bad xml based data formats without xsd to prevent someone from accidentally using schema validator for xmls.

Thanks for pointers though!

gauravk

unread,
Feb 4, 2015, 9:08:12 AM2/4/15
to golan...@googlegroups.com
Thanks, This might be useful in future as an equivalent json format is currently being designed for xml.

On Wednesday, February 4, 2015 at 6:04:52 AM UTC-5, C Banning wrote:

gauravk

unread,
Feb 4, 2015, 9:12:48 AM2/4/15
to golan...@googlegroups.com, gaurav...@gmail.com
Thanks, These are internally designed data format with no xml namespaces. So in that respect  current 'encoding/xml' should be ok. Though with SOAP requests, which I use in other projects, your changes will be greatly helpful.

Egon

unread,
Feb 4, 2015, 9:45:08 AM2/4/15
to golan...@googlegroups.com
On Wednesday, 4 February 2015 16:01:35 UTC+2, gauravk wrote:
I will try for serialization or end up writing some structs as and when needed. String() returns data in xml.
'xsd' package you mentioned would be greatly useful. Alas, nice folks here prefer designing bad xml based data formats without xsd to prevent someone from accidentally using schema validator for xmls.

I'm quite certain there is some sort of revers of convert "xml to xsd" thingy somewhere in the world available.

gauravk

unread,
Feb 4, 2015, 10:03:27 AM2/4/15
to golan...@googlegroups.com
Ah that is true. I think  xml --> xsd --> Go structs generation can cover a lot.

Thanks

Glen Newton

unread,
Feb 4, 2015, 6:50:54 PM2/4/15
to golan...@googlegroups.com
Alternate solution: chidley: convert XML to Go structs _without_ Schema
Just point chidley at each XML file and it will generate Go structs.
 Very fast.

Example uses this XML: http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=20598978,444444,455555&retmode=xml

https://github.com/gnewton/chidley
 Bonus (!!): also generates Java/JAXB ;-)

Disclaimer: chidley author

-Glen

gauravk

unread,
Feb 4, 2015, 8:09:32 PM2/4/15
to golan...@googlegroups.com
This looks seriously impressive! I am going to try later tonight or tomorrow at work.

Thanks a lot!
Reply all
Reply to author
Forward
0 new messages