https://github.com/dgrr/quickxml
Fast XML iterative (SAX) decoder in pure Go.
https://github.com/dgrr/quickxml
fast go golang parser pure xml
Last synced: 4 months ago
JSON representation
Fast XML iterative (SAX) decoder in pure Go.
- Host: GitHub
- URL: https://github.com/dgrr/quickxml
- Owner: dgrr
- License: mit
- Created: 2019-12-04T15:02:57.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-22T09:14:25.000Z (over 5 years ago)
- Last Synced: 2025-11-23T02:21:27.312Z (7 months ago)
- Topics: fast, go, golang, parser, pure, xml
- Language: Go
- Homepage:
- Size: 89.8 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Quick XML
[](https://goreportcard.com/report/github.com/dgrr/xml)
[](https://travis-ci.com/dgrr/xml)
[](https://codecov.io/gh/dgrr/xml)
QuickXML is a package to process XML files in an iterative way. It doesn't use reflect so you'll need to work a little more :D
Most of the times working with XML is a painful task. Also, the Golang std library doesn't help too much. Neither is fast nor has good doc. This library just tries to process XML files in an iterative way, ignoring most of the common errors in XML (not closing tags or putting optional tags). It just detects when a tag is open and closed (if it's closed), and it doesn't have control whether the tag X has been open before Y was closed or viceversa.
**IMPORTANT NOTE: This package doesn't provide a fully featured XML. It has been created for XLSX parsing.**
PRs are welcome.
# How QuickXML performs?

The graph shows the amount of memory used (blue) represented in the left axis.
And the time spent in seconds (red) represented in the right axis.
As you can see, QuickXML is the fastest and the second one on memory usage.in
# Example
```go
// +build ignore
package main
import (
"fmt"
"strings"
xml "github.com/dgrr/quickxml"
)
// Book represents our XML structure
type Book struct {
Category string
Title string
Authors []string
Year string
Price string
}
// String will print the book info in a string (for fmt)
func (book Book) String() string {
return fmt.Sprintf(
"%s\n Title: %s\n Authors: %s\n Year: %s\n Price: %s",
book.Category, book.Title, strings.Join(book.Authors, ", "), book.Year, book.Price,
)
}
func main() {
const str = `
Everyday Italian
Giada De Laurentiis
2005
30.00
Harry Potter
J K. Rowling
2005
29.99
XQuery Kick Start
James McGovern
Per Bothner
Kurt Cagle
James Linn
Vaidyanathan Nagarajan
2003
49.99
Learning XML
Erik T. Ray
2003
39.95
`
book := Book{}
r := xml.NewReader(strings.NewReader(str))
for r.Next() {
switch e := r.Element().(type) {
case *xml.StartElement:
switch e.Name() {
case "book": // start reading a book
attr := e.Attrs().Get("category")
if attr != nil {
book.Category = attr.Value()
}
case "title": // You can capture the lang too using e.Attrs()
r.AssignNext(&book.Title)
// AssignNext will assign the next text found to book.Title
case "author":
next := len(book.Authors)
book.Authors = append(book.Authors, "")
r.AssignNext(&book.Authors[next])
case "year":
r.AssignNext(&book.Year)
case "p:price":
r.AssignNext(&book.Price)
}
case *xml.EndElement:
if e.Name() == "book" { // book parsed
fmt.Printf("%s\n", book)
}
}
}
}
```