https://github.com/subchen/go-xmldom
XML DOM processing for Golang, supports xpath query
https://github.com/subchen/go-xmldom
dom golang xml xml-parser xpath
Last synced: 8 days ago
JSON representation
XML DOM processing for Golang, supports xpath query
- Host: GitHub
- URL: https://github.com/subchen/go-xmldom
- Owner: subchen
- License: apache-2.0
- Created: 2017-05-20T10:03:59.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-09-13T01:00:20.000Z (over 2 years ago)
- Last Synced: 2024-06-20T03:49:41.079Z (over 1 year ago)
- Topics: dom, golang, xml, xml-parser, xpath
- Language: Go
- Size: 67.4 KB
- Stars: 45
- Watchers: 3
- Forks: 27
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-xmldom
[](https://goreportcard.com/report/github.com/subchen/go-xmldom)
[](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 := `
`
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"))
```
## Create XML
```go
doc := xmldom.NewDocument("testsuites")
suiteNode := doc.Root.CreateNode("testsuite").SetAttributeValue("name", "github.com/subchen/go-xmldom")
suiteNode.CreateNode("testcase").SetAttributeValue("name", "case 1")
suiteNode.CreateNode("testcase").SetAttributeValue("name", "case 2")
fmt.Println(doc.XML())
```
## License
`go-xmldom` is released under the Apache 2.0 license. See [LICENSE](https://github.com/subchen/go-xmldom/blob/master/LICENSE)