This is a creation in Article, where the information may have evolved or changed.
Use go to do the crawler first thought of the XPath parsing to do so the internet search on GitHub found a go-write XPath library seems to be officially maintained
Official address: HTTP://GOPKG.IN/XMLPATH.V1
There are only three structural bodies in the XMLPATH.V1
Node represents the currently resolved nodes and the node is processed to be accessed by path.
Path represents the specified seek path
The collection of Iter node
It's not clear, it's a code explanation.
Package Mainimport ("FMT" "OS" Xmlpath "GOPKG.IN/XMLPATH.V1") func main () {//nodename ()//getfieldvalue () Getspecifiedvalue ()}var file *os. Filevar node *xmlpath. Nodefunc init () {var err errorfile, err = os. OpenFile ("T.xml", OS. O_RDWR, OS. MODEPERM) If err! = Nil {Panic ("OpenFile failed!!!")} Parse file gets the Nodenode of the node type that is processed and can be accessed by path, err = Xmlpath. Parse (file) if err! = Nil {Panic ("Xmlpath Parse file failed!!!")}} Func nodename () {//bookstore Gets the value of a *path type after compiling for the root node path: = Xmlpath. Mustcompile ("bookstore")//may have more than one book so use path. Iter (node) Gets the node collection under the nodes that is iteratorit: = path. Iter (node)//Determine if there is a next for it. Next () {//If you have the current node nodes out and print out the value FMT. Println (it. Node (). String ())}}func GetFieldValue () {//selects the full-text property that contains the @lang node regardless of his location path: = Xmlpath. Mustcompile ("//@lang") It: = path. Iter (node) for it. Next () {fmt. Println (it. Node (). String ())}//selects a node in the full text whose property contains lang and the value is en, regardless of his position path = Xmlpath. Mustcompile ("//*[@lang =\" en\ "]") it = path. Iter (node) for it. Next () {fmt. Println (it. Node (). String ())}}func Getspecifiedvalue () {//Select Ti for the second book in bookstoreTLE node Path: = Xmlpath. Mustcompile ("/bookstore/book[2]/title") fmt. Println (path. String (node)}
The above is a simple example that you want to drill down to see a tutorial on XPath