Go-xpath parsing

Source: Internet
Author: User
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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.