Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Go library for XML SAX (Simple API for XML) parsing

License

NotificationsYou must be signed in to change notification settings

orisano/gosax

Repository files navigation

Go Reference

gosax is a Go library for XML SAX (Simple API for XML) parsing, supporting read-only functionality. This library isdesigned for efficient and memory-conscious XML parsing, drawing inspiration from various sources to provide aperformant parser.

Features

  • Read-only SAX parsing: Stream and process XML documents without loading the entire document into memory.
  • Efficient parsing: Utilizes techniques inspired byquick-xml andpkg/json for high performance.
  • SWAR (SIMD Within A Register): Optimizations for fast text processing, inspired bymemchr.
  • Compatibility with encoding/xml: Includes utility functions to bridgegosax types withencoding/xml types, facilitating easy integration with existing code that uses the standard library.

Benchmark

goos: darwingoarch: arm64pkg: github.com/orisano/gosaxBenchmarkReader_Event-12           5 211845800 ns/op1103.30 MB/s 2097606 B/op       6 allocs/op

Installation

To installgosax, usego get:

go get github.com/orisano/gosax

Usage

Here is a basic example of how to usegosax to parse an XML document:

package mainimport ("fmt""log""strings""github.com/orisano/gosax")funcmain() {xmlData:=`<root><element>Value</element></root>`reader:=strings.NewReader(xmlData)r:=gosax.NewReader(reader)for {e,err:=r.Event()iferr!=nil {log.Fatal(err)}ife.Type()==gosax.EventEOF {break}fmt.Println(string(e.Bytes))}// Output:// <root>// <element>// Value// </element>// </root>}

Bridging with encoding/xml

Important Note for encoding/xml Users:

When migrating fromencoding/xml togosax, note that self-closing tags are handled differently. To mimicencoding/xml behavior, setgosax.Reader.EmitSelfClosingTag totrue. This ensures self-closing tags are recognized and processed correctly.

Using TokenE

If you are used toencoding/xml'sToken, start withgosax.TokenE.Note: Usinggosax.TokenE andgosax.Token involves memory allocation due to interfaces.

Before:

vardec*xml.Decoderfor {tok,err:=dec.Token()iferr==io.EOF {break}// ...}

After:

vardec*gosax.Readerfor {tok,err:=gosax.TokenE(dec.Event())iferr==io.EOF {break}// ...}

Utilizing xmlb

xmlb is an extension forgosax to simplify rewriting code fromencoding/xml. It provides a higher-performance bridge for XML parsing and processing.

Before:

vardec*xml.Decoderfor {tok,err:=dec.Token()iferr==io.EOF {break}switcht:=tok.(type) {case xml.StartElement:// ...case xml.CharData:// ...case xml.EndElement:// ...}}

After:

vardec*xmlb.Decoderfor {tok,err:=dec.Token()iferr==io.EOF {break}switchtok.Type() {casexmlb.StartElement:t,_:=tok.StartElement()// ...casexmlb.CharData:t,_:=tok.CharData()// ...casexmlb.EndElement:t:=tok.EndElement()// ...}}

License

This library is licensed under the terms specified in the LICENSE file.

Acknowledgements

gosax is inspired by the following projects and resources:

Contributing

Contributions are welcome! Please fork the repository and submit pull requests.

Contact

For any questions or feedback, feel free to open an issue on the GitHub repository.

About

Go library for XML SAX (Simple API for XML) parsing

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp