Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/camertron/xml-write-stream
An easy, streaming way to generate XML.
https://github.com/camertron/xml-write-stream
Last synced: 18 days ago
JSON representation
An easy, streaming way to generate XML.
- Host: GitHub
- URL: https://github.com/camertron/xml-write-stream
- Owner: camertron
- Created: 2015-02-23T08:10:19.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-07-01T22:51:47.000Z (over 9 years ago)
- Last Synced: 2024-11-04T00:42:04.218Z (2 months ago)
- Language: Ruby
- Size: 125 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: History.txt
Awesome Lists containing this project
README
xml-write-stream
=================[![Build Status](https://travis-ci.org/camertron/xml-write-stream.svg?branch=master)](http://travis-ci.org/camertron/xml-write-stream)
An easy, streaming way to generate XML.
## Installation
`gem install xml-write-stream`
## Usage
```ruby
require 'xml-write-stream'
```### Examples for the Impatient
There are two types of XML write stream: one that uses blocks and `yield` to write tags, and one that's purely stateful. Here are two examples that produce the same output:
Yielding:
```ruby
stream = StringIO.new
XmlWriteStream.from_stream(stream) do |writer|
writer.open_tag('foo', bar: 'baz') do |foo_writer|
foo_writer.open_tag('no-text')
foo_writer.write_text('blarg')
end
end
```Stateful:
```ruby
stream = StringIO.new
writer = XmlWriteStream.from_stream(stream)
writer.open_tag('foo', bar: 'baz')
writer.open_tag('no-text')
writer.close_tag
writer.write_text('blarg')
writer.close # automatically adds closing tags for all unclosed tags
```Output:
```ruby
stream.string # => blarg
```### Yielding Writers
As far as yielding writers go, the example above contains everything you need. The stream will be automatically closed when the outermost block terminates.
### Stateful Writers
Stateful writers have a number of additional methods:
```ruby
stream = StringIO.new
writer = XmlWriteStream.from_stream(stream)
writer.open_tag('foo')writer.eos? # => false, the stream is open and the outermost tag hasn't been closed yet
writer.open_tag # explicitly close the current tag
writer.eos? # => true, the outermost tag has been closedwriter.open_tag('foo') # => raises XmlWriteStream::EndOfStreamError
writer.closed? # => false, the stream is still open
writer.close # close the stream
writer.closed? # => true, the stream has been closed
```### Writing to a File
XmlWriteStream also supports streaming to a file via the `open` method:
Yielding:
```ruby
XmlWriteStream.open('path/to/file.xml') do |writer|
writer.open_tag('foo') do |foo_writer|
...
end
end
```Stateful:
```ruby
writer = XmlWriteStream.open('path/to/file.xml')
writer.open_tag('foo')
...
writer.close
```## Requirements
No external requirements.
## Running Tests
`bundle exec rake` should do the trick. Alternatively you can run `bundle exec rspec`, which does the same thing.
## Authors
* Cameron C. Dutro: http://github.com/camertron