Code Monkey home page Code Monkey logo

xml-stream-parser's Introduction

xml stream parser

xml-stream-parser is xml parser for GO. It is efficient to parse large xml data with streaming fashion.

Usage

<?xml version="1.0" encoding="UTF-8"?>
<bookstore number="2" loc="273456">
   <book>
      <title>The Iliad and The Odyssey</title>
      <price>12.95</price>
      <comments>
         <userComment rating="4">Best translation I've read.</userComment>
         <userComment rating="2">I like other versions better.</userComment>
      </comments>
   </book>
   <book>
      <title>Anthology of World Literature</title>
      <price>24.95</price>
      <comments>
         <userComment rating="3">Needs more modern literature.</userComment>
         <userComment rating="4">Excellent overview of world literature.</userComment>
      </comments>
   </book>
   <journal>
      <title>Journal of XML parsing</title>
      <issue>1</issue>
   </journal>
</bookstore>

Stream over books and journals

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

parser := xmlparser.NewXMLParser(br, "book").SkipElements([]string{"price", "comments"})

Attributes only

parser := xmlparser.NewXMLParser(br, "bookstore", "book").ParseAttributesOnly("bookstore")

Error handlings

for xml := range parser.Stream() {
   if xml.Err !=nil {
      // handle error
   }
}

Progress of parsing

// total byte read to calculate the progress of parsing
parser.TotalReadSize

Xpath query provides alternative to default fast access for different usecases

parser := 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 library check more examples in its documentation

If you interested check also json parser which works similarly

xml-stream-parser's People

Contributors

tamerh avatar setnicka avatar tsak avatar imirkin 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.