Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value from an XML using XPath in Go

Tags:

xml

go

xpath

Looking at go xml package I could not find such possibility. Go only allows to define tree of structures, map them to XML tree and deserialize using xml.NewDecoder(myXmlString).Decode(myStruct).

Even if I define needed tree of Go structures, I still can't query that tree using XPath.

C# has convenient function SelectSingleNode that allows to select value from XML tree by specifying XPath without duplicating whole tree structure in C# classes.

Is there similar possibility in Go ? If not then what is simplest way to implement it (possibly reusing xml package) ?

like image 887
alpav Avatar asked Nov 16 '12 03:11

alpav


People also ask

What is XPath query in XML?

XPath (XML Path Language) is a query language that can be used to query data from XML documents. In RUEI, XPath queries can be used for content scanning of XML documents.


1 Answers

There's also the xmlpath package.

Sample usage:

path := xmlpath.MustCompile("/library/book/isbn") root, err := xmlpath.Parse(file) if err != nil {     log.Fatal(err) } if value, ok := path.String(root); ok {     fmt.Println("Found:", value) } 
like image 131
rzymek Avatar answered Sep 18 '22 21:09

rzymek