xmlquery
is an XPath query package for XML document, lets you extract data or evaluate from XML documents by an XPath expression.
xmlquery
built-in the query object caching feature will caching the recently used XPATH query string. Enable caching can avoid re-compile XPath expression each query.
2019-11-11
- Add XPath query caching.
2019-10-05
- Add new methods that compatible with invalid XPath expression error:
QueryAll
andQuery
. - Add
QuerySelector
andQuerySelectorAll
methods, supported reused your query object. - PR #12 (Thanks @FrancescoIlario)
- PR #11 (Thanks @gjvnq)
2018-12-23
- added XML output will including comment node. #9
2018-12-03
- added support attribute name with namespace prefix and XML output. #6
$ go get github.com/antchfx/xmlquery
list, err := xmlquery.QueryAll(doc, "a")
if err != nil {
panic(err)
}
doc, err := xmlquery.LoadURL("http://www.example.com/sitemap.xml")
s := `<?xml version="1.0" encoding="utf-8"?><rss version="2.0"></rss>`
doc, err := xmlquery.Parse(strings.NewReader(s))
f, err := os.Open("../books.xml")
doc, err := xmlquery.Parse(f)
list := xmlquery.Find(doc, "//book//author")
// or
list := xmlquery.Find(doc, "//author")
book := xmlquery.FindOne(doc, "//book[2]")
list := xmlquery.Find(doc,"//book/@id")
list := xmlquery.Find(doc, "//book[@id='bk104']")
list := xmlquery.Find(doc, "//book[price<5]")
expr, err := xpath.Compile("sum(//book/price)")
price := expr.Evaluate(xmlquery.CreateXPathNavigator(doc)).(float64)
fmt.Printf("total price: %f\n", price)
expr, err := xpath.Compile("count(//book)")
price := expr.Evaluate(xmlquery.CreateXPathNavigator(doc)).(float64)
Find
and QueryAll
both do the same things, searches all of matched html nodes.
The Find
will panics if you give an error XPath query, but QueryAll
will return an error for you.
Yes, you can. We offer the QuerySelector
and QuerySelectorAll
methods, It will accept your query expression object.
Cache a query expression object(or reused) will avoid re-compile XPath query expression, improve your query performance.
doc := &xmlquery.Node{
Type: xmlquery.DeclarationNode,
Data: "xml",
Attr: []xml.Attr{
xml.Attr{Name: xml.Name{Local: "version"}, Value: "1.0"},
},
}
root := &xmlquery.Node{
Data: "rss",
Type: xmlquery.ElementNode,
}
doc.FirstChild = root
channel := &xmlquery.Node{
Data: "channel",
Type: xmlquery.ElementNode,
}
root.FirstChild = channel
title := &xmlquery.Node{
Data: "title",
Type: xmlquery.ElementNode,
}
title_text := &xmlquery.Node{
Data: "W3Schools Home Page",
Type: xmlquery.TextNode,
}
title.FirstChild = title_text
channel.FirstChild = title
fmt.Println(doc.OutputXML(true))
// <?xml version="1.0"?><rss><channel><title>W3Schools Home Page</title></channel></rss>
import (
"github.com/antchfx/xmlquery"
)
func main(){
s := `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>https://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>https://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
<item>
<title>XML Tutorial</title>
<link>https://www.w3schools.com/xml</link>
<description>New XML tutorial on W3Schools</description>
</item>
</channel>
</rss>`
doc, err := xmlquery.Parse(strings.NewReader(s))
if err != nil {
panic(err)
}
channel := xmlquery.FindOne(doc, "//channel")
if n := channel.SelectElement("title"); n != nil {
fmt.Printf("title: %s\n", n.InnerText())
}
if n := channel.SelectElement("link"); n != nil {
fmt.Printf("link: %s\n", n.InnerText())
}
for i, n := range xmlquery.Find(doc, "//item/title") {
fmt.Printf("#%d %s\n", i, n.InnerText())
}
}
Name | Description |
---|---|
htmlquery | XPath query package for the HTML document |
xmlquery | XPath query package for the XML document |
jsonquery | XPath query package for the JSON document |
Please let me know if you have any questions