Skip to content

Commit

Permalink
Add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
subchen committed Feb 2, 2017
1 parent d03b129 commit aef1655
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# go-xmldom

[![Go Report Card](https://goreportcard.com/badge/github.com/subchen/go-xmldom)](https://goreportcard.com/report/github.com/subchen/go-xmldom)
[![GoDoc](https://godoc.org/github.com/subchen/go-xmldom?status.svg)](https://godoc.org/github.com/subchen/go-xmldom)

XML DOM processing for Golang, supports xpath query

* Parse XML into dom
* Query node using xpath
* Update XML using dom

## Installation

```bash
$ go get github.com/subchen/go-xmldom
```

## Basic Usage

```go
xml = `<testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom">
<testcase classname="go-xmldom" name="ExampleParseXML" time="0.004"></testcase>
<testcase classname="go-xmldom" name="ExampleParse" time="0.005"></testcase>
</testsuite>`

doc := xmldom.Must(xmldom.ParseXML(xml))
root := doc.Root

name := root.GetAttributeValue("name")
time := root.GetAttributeValue("time")
fmt.Printf("testsuite: name=%v, time=%v\n", name, time)

for _, node := range root.GetChildren("testcase") {
name := node.GetAttributeValue("name")
time := node.GetAttributeValue("time")
fmt.Printf("testcase: name=%v, time=%v\n", name, time)
}
```

## Xpath Query

```go
// find all children
fmt.Printf("children = %v\n", len(node.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"))
}

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

## License

Apache 2

0 comments on commit aef1655

Please sign in to comment.