Git Product home page Git Product logo

xmlstream's Introduction

Overview

Lightweight XML scanner built on top of Go's encoding/xml. It keeps the flexibility of xml.Unmarshal while allowing the parsing of huge XML files.

Documentation: http://godoc.org/github.com/srom/xmlstream

Usage

Say you want to parse the following XML file:

<Person>
	<FullName>Jon Arbuckle</FullName>
	<Email where="home">
		<Addr>[email protected]</Addr>
	</Email>
	<Email where='work'>
		<Addr>[email protected]</Addr>
	</Email>
</Person>

<Cat>
	<Nickname>Garfield</Nickname>
	<Breed>Red British Shorthair</Breed>
</Cat>

<Person>
	<FullName>Dr. Liz Wilson</FullName>
	<Email where="home">
		<Addr>[email protected]</Addr>
	</Email>
	<Email where='work'>
		<Addr>[email protected]</Addr>
	</Email>
</Person>

Step 1 → Define your struct objects like you would usually do it to use xml.Unmarshal.

type Person struct {
	Name   string  `xml:"FullName"`
	Emails []Email `xml:"Email"`
}
type Email struct {
	Where string `xml:"where,attr"`
	Addr  string
}

type Cat struct {
	Name  string `xml:"Nickname"`
	Breed string
}

Step 2 → Create a new xmlstream.Scanner and iterate through it:

scanner := xmlstream.NewScanner(os.Stdin, new(Person), new(Cat))

personCounter := 0
catCounter := 0
for scanner.Scan() {
  tag := scanner.Element()
  switch el := tag.(type) {
  case *Person:
    person := *el
    personCounter++
    fmt.Printf("Human N°%d: %s, %s\n", personCounter, person.Name, person.Emails)

  case *Cat:
    cat := *el
    catCounter++
    fmt.Printf("Cat N°%d: %s, %s\n", catCounter, cat.Name, cat.Breed)
  }
}

if err := scanner.Err(); err != nil {
  t.Errorf("Error while scanning XML: %v\n", err)
}

Output:

Human N°1: Jon Arbuckle, [{home [email protected]} {work [email protected]}]
Cat N°1: Garfield, Red British Shorthair
Human N°2: Dr. Liz Wilson, [{home [email protected]} {work [email protected]}]

About & License

Built by Romain Strock under the ☀ of London.

Released under MIT License.

xmlstream's People

Contributors

srom avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.