moovweb/gokogiri

how use ?

Closed this issue · 5 comments

how use ?

Hi @mejinke, what exactly are you trying to accomplish with gokogiri? If you give me an example, I could provide a possible use case.

I didn't want to make a new post. From reading the source \ blog post about Gokogiri I know that I can do this:

xml, _ := ioutil.ReadFile("the.xml")
doc, _ := gokogiri.ParseXml([]byte(xml))
defer doc.Free()

res, _ := doc.Search("//title")
for _, title := range res {
    fmt.Println(title)
}

Is there a way to use css selectors? Can I do that inside of the Search() function? A simple 'synopsis' section like the one found on the Nokogiri page would be super helpful in the Gokogiri README.

@jcswart As of right now, the best way to use CSS selectors is to convert them into xpath. The gokogiri/css package contains a conversion function for just this purpose. Thus, you can write your above example as follows:

...
import "gokogiri/css"
...

xml, _ := ioutil.ReadFile("the.xml")
doc, _ := gokogiri.ParseXml([]byte(xml))
defer doc.Free()

xpath := css.Convert(".title", doc)
res, _ := doc.Search(xpath)
for _, title := range res {
    fmt.Println(title)
}

Awesome thanks for the response! I stumbled upon this in the source and half-way figured out this was the approach to take. Thanks for putting it all together!

Yup, no problem =)