-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
47 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,63 @@ | ||
# goxpp | ||
|
||
[![Build Status](https://github.com/mmcdole/goxpp/actions/workflows/ci.yml/badge.svg)](https://github.com/mmcdole/goxpp/actions/workflows/ci.yml) | ||
[![codecov](https://codecov.io/gh/mmcdole/goxpp/branch/main/graph/badge.svg)](https://codecov.io/gh/mmcdole/goxpp) | ||
[![codecov](https://codecov.io/gh/mmcdole/goxpp/branch/master/graph/badge.svg)](https://codecov.io/gh/mmcdole/goxpp) | ||
[![License](http://img.shields.io/:license-mit-blue.svg)](http://doge.mit-license.org) | ||
[![Go Reference](https://pkg.go.dev/badge/github.com/mmcdole/goxpp.svg)](https://pkg.go.dev/github.com/mmcdole/goxpp) | ||
|
||
The `goxpp` library, inspired by [Java's XMLPullParser](http://www.xmlpull.org/v1/download/unpacked/doc/quick_intro.html), is a lightweight wrapper for Go's standard XML Decoder, tailored for developers who need fine-grained control over XML parsing. Unlike simple unmarshaling of entire documents, this library excels in scenarios requiring manual navigation and consumption of XML elements. It provides a pull parser approach with convenience methods for effortlessly consuming whole tags, skipping elements, and more, granting a level of flexibility and control beyond what Go's standard XML decode method offers. | ||
A lightweight XML Pull Parser for Go, inspired by [Java's XMLPullParser](http://www.xmlpull.org/v1/download/unpacked/doc/quick_intro.html). It provides fine-grained control over XML parsing with a simple, intuitive API. | ||
|
||
## Overview | ||
## Features | ||
|
||
To begin parsing a XML document using `goxpp` you must pass it an `io.Reader` object for your document: | ||
- Pull-based parsing for fine-grained document control | ||
- Efficient navigation and element skipping | ||
- Simple, idiomatic Go API | ||
|
||
```go | ||
file, err := os.Open("path/file.xml") | ||
parser := xpp.NewXMLPullParser(file, false, charset.NewReader) | ||
## Installation | ||
|
||
```bash | ||
go get github.com/mmcdole/goxpp | ||
``` | ||
|
||
The `goxpp` library decodes documents into a series of token objects: | ||
## Quick Start | ||
|
||
```go | ||
import "github.com/mmcdole/goxpp" | ||
|
||
// Create a new parser | ||
file, _ := os.Open("file.xml") | ||
parser := xpp.NewXMLPullParser(file, false, nil) | ||
|
||
// Navigate through the document | ||
for { | ||
token, _ := parser.Next() | ||
if token == xpp.EndDocument { | ||
break | ||
} | ||
|
||
switch token { | ||
case xpp.StartTag: | ||
// Handle start tag | ||
fmt.Printf("Tag: %s\n", parser.Name) | ||
case xpp.Text: | ||
// Handle text content | ||
fmt.Printf("Text: %s\n", parser.Text) | ||
} | ||
} | ||
``` | ||
|
||
| Token Name | | ||
|----------------------------------| | ||
| StartDocument | | ||
| EndDocument | | ||
| StartTag | | ||
| EndTag | | ||
| Text | | ||
| Comment | | ||
| ProcessingInstruction | | ||
| Directive | | ||
| IgnorableWhitespace | | ||
## Token Types | ||
|
||
You will always start at the `StartDocument` token and can use the following functions to walk through a document: | ||
- `StartDocument`, `EndDocument` | ||
- `StartTag`, `EndTag` | ||
- `Text`, `Comment` | ||
- `ProcessingInstruction`, `Directive` | ||
- `IgnorableWhitespace` | ||
|
||
| Function Name | Description | | ||
|----------------------------------|---------------------------------------| | ||
| Next() | Advance to the next `Text`, `StartTag`, `EndTag`, `EndDocument` token.<br>Note: skips `Comment`, `Directive` and `ProcessingInstruction` | | ||
| NextToken() | Advance to the next token regardless of type. | | ||
| NextText() | Advance to the next `Text` token. | | ||
| Skip() | Skip the next token. | | ||
| DecodeElement(v interface{}) | Decode an entire element from the current tag into a struct.<br>Note: must be at a `StartTag` token | | ||
## Documentation | ||
|
||
For detailed documentation and examples, visit [pkg.go.dev](https://pkg.go.dev/github.com/mmcdole/goxpp). | ||
|
||
## License | ||
|
||
This project is licensed under the [MIT License](https://raw.githubusercontent.com/mmcdole/goxpp/master/LICENSE) | ||
This project is licensed under the [MIT License](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/mmcdole/goxpp | ||
|
||
go 1.21 | ||
go 1.19 | ||
|
||
require github.com/stretchr/testify v1.10.0 | ||
|
||
|