{"id":15687830,"url":"https://github.com/orisano/gosax","last_synced_at":"2025-04-05T12:04:33.788Z","repository":{"id":246172434,"uuid":"820222949","full_name":"orisano/gosax","owner":"orisano","description":"Go library for XML SAX (Simple API for XML) parsing","archived":false,"fork":false,"pushed_at":"2025-03-14T18:11:24.000Z","size":32,"stargazers_count":133,"open_issues_count":2,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T11:06:19.969Z","etag":null,"topics":["go","sax","xml"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/orisano.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-26T03:49:42.000Z","updated_at":"2025-03-20T03:34:36.000Z","dependencies_parsed_at":"2024-10-23T20:42:05.252Z","dependency_job_id":null,"html_url":"https://github.com/orisano/gosax","commit_stats":null,"previous_names":["orisano/gosax"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orisano%2Fgosax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orisano%2Fgosax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orisano%2Fgosax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orisano%2Fgosax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/orisano","download_url":"https://codeload.github.com/orisano/gosax/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247332601,"owners_count":20921853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","sax","xml"],"created_at":"2024-10-03T17:51:17.634Z","updated_at":"2025-04-05T12:04:33.746Z","avatar_url":"https://github.com/orisano.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gosax\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/orisano/gosax.svg)](https://pkg.go.dev/github.com/orisano/gosax)\n\n`gosax` is a Go library for XML SAX (Simple API for XML) parsing, supporting read-only functionality. This library is\ndesigned for efficient and memory-conscious XML parsing, drawing inspiration from various sources to provide a\nperformant parser.\n\n## Features\n\n- **Read-only SAX parsing**: Stream and process XML documents without loading the entire document into memory.\n- **Efficient parsing**: Utilizes techniques inspired by `quick-xml` and `pkg/json` for high performance.\n- **SWAR (SIMD Within A Register)**: Optimizations for fast text processing, inspired by `memchr`.\n- **Compatibility with encoding/xml**: Includes utility functions to bridge `gosax` types with `encoding/xml` types, facilitating easy integration with existing code that uses the standard library.\n\n## Benchmark\n```\ngoos: darwin\ngoarch: arm64\npkg: github.com/orisano/gosax\nBenchmarkReader_Event-12    \t       5\t 211845800 ns/op\t1103.30 MB/s\t 2097606 B/op\t       6 allocs/op\n```\n\n## Installation\n\nTo install `gosax`, use `go get`:\n\n```bash\ngo get github.com/orisano/gosax\n```\n\n## Usage\n\nHere is a basic example of how to use `gosax` to parse an XML document:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"strings\"\n\n\t\"github.com/orisano/gosax\"\n)\n\nfunc main() {\n\txmlData := `\u003croot\u003e\u003celement\u003eValue\u003c/element\u003e\u003c/root\u003e`\n\treader := strings.NewReader(xmlData)\n\n\tr := gosax.NewReader(reader)\n\tfor {\n\t\te, err := r.Event()\n\t\tif err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tif e.Type() == gosax.EventEOF {\n\t\t\tbreak\n\t\t}\n\t\tfmt.Println(string(e.Bytes))\n\t}\n\t// Output:\n\t// \u003croot\u003e\n\t// \u003celement\u003e\n\t// Value\n\t// \u003c/element\u003e\n\t// \u003c/root\u003e\n}\n\n```\n\n### Bridging with encoding/xml\n\n**Important Note for encoding/xml Users:**\n\u003e When migrating from `encoding/xml` to `gosax`, note that self-closing tags are handled differently. To mimic `encoding/xml` behavior, set `gosax.Reader.EmitSelfClosingTag` to `true`. This ensures self-closing tags are recognized and processed correctly.\n\n#### Using TokenE\nIf you are used to `encoding/xml`'s `Token`, start with `gosax.TokenE`. \n**Note:** Using `gosax.TokenE` and `gosax.Token` involves memory allocation due to interfaces.\n\n**Before:**\n```go\nvar dec *xml.Decoder\nfor {\n\ttok, err := dec.Token()\n\tif err == io.EOF {\n\t\tbreak\n\t}\n\t// ...\n}\n```\n\n**After:**\n```go\nvar dec *gosax.Reader\nfor {\n\ttok, err := gosax.TokenE(dec.Event())\n\tif err == io.EOF {\n\t\tbreak\n\t}\n\t// ...\n}\n```\n\n#### Utilizing xmlb\n`xmlb` is an extension for `gosax` to simplify rewriting code from `encoding/xml`. It provides a higher-performance bridge for XML parsing and processing.\n\n**Before:**\n```go\nvar dec *xml.Decoder\nfor {\n\ttok, err := dec.Token()\n\tif err == io.EOF {\n\t\tbreak\n\t}\n\tswitch t := tok.(type) {\n\tcase xml.StartElement:\n\t\t// ...\n\tcase xml.CharData:\n\t\t// ...\n\tcase xml.EndElement:\n\t\t// ...\n\t}\n} \n```\n\n**After:**\n```go\nvar dec *xmlb.Decoder\nfor {\n\ttok, err := dec.Token()\n\tif err == io.EOF {\n\t\tbreak\n\t}\n\tswitch tok.Type() {\n\tcase xmlb.StartElement:\n\t\tt, _ := tok.StartElement()\n\t\t// ...\n\tcase xmlb.CharData:\n\t\tt, _ := tok.CharData()\n\t\t// ...\n\tcase xmlb.EndElement:\n\t\tt := tok.EndElement()\n\t\t// ...\n\t}\n} \n```\n\n## License\n\nThis library is licensed under the terms specified in the LICENSE file.\n\n## Acknowledgements\n\n`gosax` is inspired by the following projects and resources:\n\n- [Dave Cheney's GopherCon SG 2023 Talk](https://dave.cheney.net/paste/gophercon-sg-2023.html)\n- [quick-xml](https://github.com/tafia/quick-xml)\n- [memchr](https://github.com/BurntSushi/memchr) (SWAR part)\n\n## Contributing\n\nContributions are welcome! Please fork the repository and submit pull requests.\n\n## Contact\n\nFor any questions or feedback, feel free to open an issue on the GitHub repository.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forisano%2Fgosax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Forisano%2Fgosax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forisano%2Fgosax/lists"}