An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Quick XML

[![Go Report Card](https://goreportcard.com/badge/github.com/dgrr/xml)](https://goreportcard.com/report/github.com/dgrr/xml)
[![Build Status](https://travis-ci.com/dgrr/xml.svg?branch=master)](https://travis-ci.com/dgrr/xml)
[![codecov](https://codecov.io/gh/dgrr/xml/branch/master/graph/badge.svg)](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?

![alt_text](https://github.com/dgrr/quickxml/blob/master/_imgs/alloc_time.png)

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)
}
}
}
}
```