Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tamerh/xml-stream-parser
XML stream parser for GO
https://github.com/tamerh/xml-stream-parser
go parser xml xml-deserialization xml-parsing xml-stream-parser
Last synced: about 1 month ago
JSON representation
XML stream parser for GO
- Host: GitHub
- URL: https://github.com/tamerh/xml-stream-parser
- Owner: tamerh
- License: bsd-3-clause
- Created: 2019-01-23T22:41:07.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-02-03T08:24:08.000Z (11 months ago)
- Last Synced: 2024-06-25T17:59:51.032Z (6 months ago)
- Topics: go, parser, xml, xml-deserialization, xml-parsing, xml-stream-parser
- Language: Go
- Homepage:
- Size: 185 KB
- Stars: 104
- Watchers: 7
- Forks: 14
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# xml stream parser
xml-stream-parser is xml parser for GO. It is efficient to parse large xml data with streaming fashion.
## Usage
```xml
The Iliad and The Odyssey
12.95
Best translation I've read.
I like other versions better.
Anthology of World Literature
24.95
Needs more modern literature.
Excellent overview of world literature.
Journal of XML parsing
1
```
**Stream** over books and journals
```go
f, _ := os.Open("input.xml")
br := bufio.NewReaderSize(f,65536)
parser := xmlparser.NewXMLParser(br, "book", "journal")for xml := range parser.Stream() {
fmt.Println(xml.Childs["title"][0].InnerText)
if xml.Name == "book" {
fmt.Println(xml.Childs["comments"][0].Childs["userComment"][0].Attrs["rating"])
fmt.Println(xml.Childs["comments"][0].Childs["userComment"][0].InnerText)
}
}
```**Skip** tags for speed
```go
parser := xmlparser.NewXMLParser(br, "book").SkipElements([]string{"price", "comments"})
```**Attributes** only
```go
parser := xmlparser.NewXMLParser(br, "bookstore", "book").ParseAttributesOnly("bookstore")
```**Error** handlings
```go
for xml := range parser.Stream() {
if xml.Err !=nil {
// handle error
}
}
```**Progress** of parsing
```go
// total byte read to calculate the progress of parsing
parser.TotalReadSize
```**Xpath** query provides alternative to default fast access for different usecases
```goparser := xmlparser.NewXMLParser(bufreader, "bookstore").EnableXpath()
for xml := range p.Stream() {
// select books
xml.SelectElements("//book")
xml.SelectElements("./book")
xml.SelectElements("book")
// select titles
xml.SelectElements("./book/title")
// select book with price condition
xml.SelectElements("//book[price>=20.95]"))
//comments with rating 4
xml.SelectElements("//book/comments/userComment[@rating='4']")
}
// for evaluate function or reuse existing xpath expression
// sum of all the book price
expr, err := p.CompileXpath("sum(//book/price)")
price := expr.Evaluate(p.CreateXPathNavigator(xml)).(float64)```
xpath functionality implemented via [xpath](https://github.com/antchfx/xpath) library check more
examples in its documentationIf you interested check also [json parser](https://github.com/tamerh/jsparser) which works similarly