Skip to content

Commit

Permalink
Add node.FindByName(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
subchen committed May 24, 2017
1 parent 81f0adf commit 1cc1b4b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
55 changes: 41 additions & 14 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const (
<properties>
<property name="go.version">go1.8.1</property>
</properties>
<testcase classname="go-xmldom" name="ExampleParseXML" time="0.004"></testcase>
<testcase classname="go-xmldom" name="ExampleParse" time="0.005"></testcase>
<testcase classname="go-xmldom" id="ExampleParseXML" time="0.004"></testcase>
<testcase classname="go-xmldom" id="ExampleParse" time="0.005"></testcase>
</testsuite>
</testsuites>`
)
Expand Down Expand Up @@ -45,11 +45,38 @@ func ExampleNode_GetChildren() {
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
children := node.FirstChild().GetChildren("testcase")
for _, c := range children {
fmt.Printf("%v: name = %v\n", c.Name, c.GetAttributeValue("name"))
fmt.Printf("%v: id = %v\n", c.Name, c.GetAttributeValue("id"))
}
// Output:
// testcase: name = ExampleParseXML
// testcase: name = ExampleParse
// testcase: id = ExampleParseXML
// testcase: id = ExampleParse
}

func ExampleNode_FindByID() {
root := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
node := root.FindByID("ExampleParseXML")
fmt.Println(node.XML())
// Output:
// <testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" />
}

func ExampleNode_FindOneByName() {
root := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
node := root.FindOneByName("property")
fmt.Println(node.XML())
// Output:
// <property name="go.version">go1.8.1</property>
}

func ExampleNode_FindByName() {
root := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
nodes := root.FindByName("testcase")
for _, node := range nodes {
fmt.Println(node.XML())
}
// Output:
// <testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" />
// <testcase classname="go-xmldom" id="ExampleParse" time="0.005" />
}

func ExampleNode_Query() {
Expand All @@ -62,30 +89,30 @@ func ExampleNode_Query() {
// find node matched tag name
nodeList := node.Query("//testcase")
for _, c := range nodeList {
fmt.Printf("%v: name = %v\n", c.Name, c.GetAttributeValue("name"))
fmt.Printf("%v: id = %v\n", c.Name, c.GetAttributeValue("id"))
}
// Output:
// children = 5
// testcase: name = ExampleParseXML
// testcase: name = ExampleParse
// testcase: id = ExampleParseXML
// testcase: id = ExampleParse
}

func ExampleNode_QueryOne() {
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
// xpath expr: https://github.com/antchfx/xpath

// find node matched attr name
c := node.QueryOne("//testcase[@name='ExampleParseXML']")
fmt.Printf("%v: name = %v\n", c.Name, c.GetAttributeValue("name"))
c := node.QueryOne("//testcase[@id='ExampleParseXML']")
fmt.Printf("%v: id = %v\n", c.Name, c.GetAttributeValue("id"))
// Output:
// testcase: name = ExampleParseXML
// testcase: id = ExampleParseXML
}

func ExampleDocument_XML() {
doc := xmldom.Must(xmldom.ParseXML(ExampleXml))
fmt.Println(doc.XML())
// Output:
// <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE junit SYSTEM "junit-result.dtd"><testsuites><testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom"><properties><property name="go.version">go1.8.1</property></properties><testcase classname="go-xmldom" name="ExampleParseXML" time="0.004" /><testcase classname="go-xmldom" name="ExampleParse" time="0.005" /></testsuite></testsuites>
// <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE junit SYSTEM "junit-result.dtd"><testsuites><testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom"><properties><property name="go.version">go1.8.1</property></properties><testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" /><testcase classname="go-xmldom" id="ExampleParse" time="0.005" /></testsuite></testsuites>
}

func ExampleDocument_XMLPretty() {
Expand All @@ -99,8 +126,8 @@ func ExampleDocument_XMLPretty() {
// <properties>
// <property name="go.version">go1.8.1</property>
// </properties>
// <testcase classname="go-xmldom" name="ExampleParseXML" time="0.004" />
// <testcase classname="go-xmldom" name="ExampleParse" time="0.005" />
// <testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" />
// <testcase classname="go-xmldom" id="ExampleParse" time="0.005" />
// </testsuite>
// </testsuites>
}
Expand Down
32 changes: 31 additions & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,42 @@ func (n *Node) FindByID(id string) *Node {
}

for _, c := range n.Children {
return c.FindByID(id)
if x := c.FindByID(id); x != nil {
return x
}
}

return nil
}

func (n *Node) FindOneByName(name string) *Node {
if n.Name == name {
return n
}

for _, c := range n.Children {
if x := c.FindOneByName(name); x != nil {
return x
}
}

return nil
}

func (n *Node) FindByName(name string) []*Node {
var nodes []*Node

if n.Name == name {
nodes = append(nodes, n)
}

for _, c := range n.Children {
nodes = append(nodes, c.FindByName(name)...)
}

return nodes
}

func (n *Node) Query(xpath string) []*Node {
return xpathQuery(n, xpath)
}
Expand Down

0 comments on commit 1cc1b4b

Please sign in to comment.