Skip to content

Commit 3b69d31

Browse files
committed
add new methods:QueryAll(), Query(), QuerySelectorAll(), QuerySelector()
1 parent a2896be commit 3b69d31

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ doc, err := jsonquery.Parse(f)
4646
list := jsonquery.Find(doc, "store/book/*/author")
4747
// or equal to
4848
list := jsonquery.Find(doc, "//author")
49+
// or by QueryAll()
50+
nodes, err := jsonquery.QueryAll(doc, "//a")
4951
```
5052

5153
#### Find the third book.
@@ -72,7 +74,7 @@ list := jsonquery.Find(doc, "//book/*[isbn]")
7274
list := jsonquery.Find(doc, "//book/*[price<10]")
7375
```
7476

75-
Quick Tutorial
77+
Examples
7678
===
7779

7880
```go
@@ -96,7 +98,10 @@ func main() {
9698
}
9799
]
98100
}`
99-
doc, _ := jsonquery.Parse(strings.NewReader(s))
101+
doc, err := jsonquery.Parse(strings.NewReader(s))
102+
if err != nil {
103+
panic(err)
104+
}
100105
name := jsonquery.FindOne(doc, "name")
101106
fmt.Printf("name: %s\n", name.InnerText())
102107
var a []string
@@ -160,7 +165,7 @@ The above JSON document will be convert to similar to XML document by the *JSONQ
160165

161166
Notes: `element` is empty element that have no any name.
162167

163-
List of supported XPath query packages
168+
List of XPath query packages
164169
===
165170
|Name |Description |
166171
|--------------------------|----------------|

query.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,61 @@ func CreateXPathNavigator(top *Node) *NodeNavigator {
1313
return &NodeNavigator{cur: top, root: top}
1414
}
1515

16-
// Find searches the Node that matches by the specified XPath expr.
16+
// Find is like QueryAll but will panics if `expr` cannot be parsed.
1717
func Find(top *Node, expr string) []*Node {
18-
exp, err := xpath.Compile(expr)
18+
nodes, err := QueryAll(top, expr)
1919
if err != nil {
2020
panic(err)
2121
}
22-
t := exp.Select(CreateXPathNavigator(top))
23-
var elems []*Node
24-
for t.MoveNext() {
25-
elems = append(elems, (t.Current().(*NodeNavigator)).cur)
22+
return nodes
23+
}
24+
25+
// FindOne is like Query but will panics if `expr` cannot be parsed.
26+
func FindOne(top *Node, expr string) *Node {
27+
node, err := Query(top, expr)
28+
if err != nil {
29+
panic(err)
2630
}
27-
return elems
31+
return node
32+
}
33+
34+
// QueryAll searches the Node that matches by the specified XPath expr.
35+
// Return an error if the expression `expr` cannot be parsed.
36+
func QueryAll(top *Node, expr string) ([]*Node, error) {
37+
exp, err := xpath.Compile(expr)
38+
if err != nil {
39+
return nil, err
40+
}
41+
return QuerySelectorAll(top, exp), nil
2842
}
2943

30-
// FindOne searches the Node that matches by the specified XPath expr,
44+
// Query searches the Node that matches by the specified XPath expr,
3145
// and returns first element of matched.
32-
func FindOne(top *Node, expr string) *Node {
46+
func Query(top *Node, expr string) (*Node, error) {
3347
exp, err := xpath.Compile(expr)
3448
if err != nil {
35-
panic(err)
49+
return nil, err
3650
}
37-
t := exp.Select(CreateXPathNavigator(top))
38-
var elem *Node
51+
return QuerySelector(top, exp), nil
52+
}
53+
54+
// QuerySelectorAll searches all of the Node that matches the specified XPath selectors.
55+
func QuerySelectorAll(top *Node, selector *xpath.Expr) []*Node {
56+
t := selector.Select(CreateXPathNavigator(top))
57+
var elems []*Node
58+
for t.MoveNext() {
59+
elems = append(elems, (t.Current().(*NodeNavigator)).cur)
60+
}
61+
return elems
62+
}
63+
64+
// QuerySelector returns the first matched XML Node by the specified XPath selector.
65+
func QuerySelector(top *Node, selector *xpath.Expr) *Node {
66+
t := selector.Select(CreateXPathNavigator(top))
3967
if t.MoveNext() {
40-
elem = (t.Current().(*NodeNavigator)).cur
68+
return (t.Current().(*NodeNavigator)).cur
4169
}
42-
return elem
70+
return nil
4371
}
4472

4573
// NodeNavigator is for navigating JSON document.

0 commit comments

Comments
 (0)