xml

package module
v0.0.0-...-4977de5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2020 License: MIT Imports: 6 Imported by: 9

README

Quick XML

Go Report Card Build Status codecov

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

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

// +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 = `<bookstore xmlns:p="urn:schemas-books-com:prices">

	<book category="COOKING">
	  <title lang="en">Everyday Italian</title>
	  <author>Giada De Laurentiis</author>
	  <year>2005</year>
	  <p:price>30.00</p:price>
	</book>
  
	<book category="CHILDREN">
	  <title lang="en">Harry Potter</title>
	  <author>J K. Rowling</author>
	  <year>2005</year>
	  <p:price>29.99</p:price>
	</book>
  
	<book category="WEB">
	  <title lang="en">XQuery Kick Start</title>
	  <author>James McGovern</author>
	  <author>Per Bothner</author>
	  <author>Kurt Cagle</author>
	  <author>James Linn</author>
	  <author>Vaidyanathan Nagarajan</author>
	  <year>2003</year>
	  <p:price>49.99</p:price>
	</book>
  
	<book category="WEB">
	  <title lang="en">Learning XML</title>
	  <author>Erik T. Ray</author>
	  <year>2003</year>
	  <p:price>39.95</p:price>
	</book>
  
  </bookstore>`

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attrs

type Attrs []KV

Attrs represents the attributes of an XML StartElement.

func NewAttrs

func NewAttrs(attrs ...string) *Attrs

NewAttr creates a new attribyte list.

The attributes are a key-value pair of strings. For example: NewAttrs("k", "v") will create the attr k="v".

If the attrs are odd nothing happens. The value associated with that key will be empty.

func (*Attrs) CopyTo

func (kvs *Attrs) CopyTo(kv2 *Attrs)

CopyTo copies kvs to kv2.

func (*Attrs) Get

func (kvs *Attrs) Get(name string) *KV

Get returns the attribute based on name.

If the name doesn't match any of the keys KV will be nil.

func (*Attrs) GetBytes

func (kvs *Attrs) GetBytes(name []byte) *KV

GetBytes returns the attribute based on name.

If the name doesn't match any of the keys KV will be nil.

func (*Attrs) Len

func (kvs *Attrs) Len() int

Len returns the number of attributes.

func (*Attrs) Range

func (kvs *Attrs) Range(fn func(kv *KV))

Range passes every attr to fn.

func (*Attrs) RangePre

func (kvs *Attrs) RangePre(fn func(kv *KV) bool)

RangePre passes every attr to fn.

If fn returns false the range loop will break.

func (*Attrs) RangeWithIndex

func (kvs *Attrs) RangeWithIndex(fn func(i int, kv *KV))

RangeWithIndex passes every attr and the index to fn.

type Element

type Element interface {
	String() string
	// contains filtered or unexported methods
}

Element represents a XML element.

Element can be: - StartElement. - EndElement. - TextElement.

type EndElement

type EndElement struct {
	// contains filtered or unexported fields
}

EndElement represents a XML end element.

func NewEnd

func NewEnd(name string) *EndElement

NewEnd creates a new EndElement.

func (*EndElement) Name

func (e *EndElement) Name() string

Name returns the name of the XML node.

func (*EndElement) NameBytes

func (e *EndElement) NameBytes() []byte

NameBytes returns the name of the XML node in bytes.

func (*EndElement) NameUnsafe

func (e *EndElement) NameUnsafe() string

NameUnsafe returns a string holding the name parameter.

This function differs from Name() on using unsafe methods.

func (*EndElement) Reset

func (e *EndElement) Reset()

func (*EndElement) SetName

func (e *EndElement) SetName(name string)

SetName sets the name to the end element.

func (*EndElement) SetNameBytes

func (e *EndElement) SetNameBytes(name []byte)

SetNameBytes sets the name to the end element in bytes.

func (*EndElement) String

func (e *EndElement) String() string

String returns the string representation of EndElement.

type KV

type KV struct {
	// contains filtered or unexported fields
}

KV represents an attr which is a key-value pair.

func (*KV) Key

func (kv *KV) Key() string

Key returns the key.

func (*KV) KeyBytes

func (kv *KV) KeyBytes() []byte

KeyBytes returns the key.

func (*KV) KeyUnsafe

func (kv *KV) KeyUnsafe() string

KeyUnsafe returns a string holding the name parameter.

This function differs from Key() on using unsafe methods.

func (*KV) Value

func (kv *KV) Value() string

Value returns the value.

func (*KV) ValueBytes

func (kv *KV) ValueBytes() []byte

ValueBytes returns the value.

func (*KV) ValueUnsafe

func (kv *KV) ValueUnsafe() string

ValueUnsafe returns a string holding the name parameter.

This function differs from Value() on using unsafe methods.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader represents a XML reader.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a initialized reader.

func (*Reader) AssignNext

func (r *Reader) AssignNext(ptr *string)

AssignNext will assign the next TextElement to ptr.

func (*Reader) Element

func (r *Reader) Element() Element

Element returns the last readed element.

func (*Reader) Error

func (r *Reader) Error() error

Error return the last error.

func (*Reader) Next

func (r *Reader) Next() bool

Next iterates until the next XML element.

type StartElement

type StartElement struct {
	// contains filtered or unexported fields
}

StartElement represents the start of a XML node.

func NewStart

func NewStart(name string, hasEnd bool, attrs *Attrs) *StartElement

NewStart creats a new StartElement.

func (*StartElement) Attrs

func (s *StartElement) Attrs() *Attrs

Attrs returns the attributes of an element.

func (*StartElement) HasEnd

func (s *StartElement) HasEnd() bool

HasEnd indicates if the StartElement ends as /> Having this true means we do not expect a EndElement.

func (*StartElement) Name

func (s *StartElement) Name() string

Name returns the name of the element.

func (*StartElement) NameBytes

func (s *StartElement) NameBytes() []byte

NameBytes returns the name of the element.

func (*StartElement) NameUnsafe

func (s *StartElement) NameUnsafe() string

NameUnsafe returns a string holding the name parameter.

This function differs from Name() on using unsafe methods.

func (*StartElement) Reset

func (s *StartElement) Reset()

Reset sets the default values to the StartElement.

func (*StartElement) SetName

func (s *StartElement) SetName(name string)

SetName sets a string as StartElement's name.

func (*StartElement) SetNameBytes

func (s *StartElement) SetNameBytes(name []byte)

SetNameBytes sets the name bytes to the StartElement.

func (*StartElement) String

func (s *StartElement) String() string

type TextElement

type TextElement string

TextElement represents a XML text.

func NewText

func NewText(str string) *TextElement

NewText creates a new TextElement.

func (*TextElement) String

func (t *TextElement) String() string

String returns the string representation of TextElement.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is used to write the XML elements.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new XML writer.

func (*Writer) Write

func (w *Writer) Write(e Element) error

Write writes the parsed element.

func (*Writer) WriteIndent

func (w *Writer) WriteIndent(e Element) error

WriteIndent writes the parsed element indentating the elements.

Directories

Path Synopsis
+build ignore +build ignore
+build ignore +build ignore
examples
reader
+build ignore
+build ignore
writer
+build ignore
+build ignore

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL