2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
[![Arduino CI](https://github.com/RobTillaart/XMLWriter/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
2021-12-29 10:27:03 -05:00
|
|
|
[![Arduino-lint](https://github.com/RobTillaart/XMLWriter/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/XMLWriter/actions/workflows/arduino-lint.yml)
|
|
|
|
[![JSON check](https://github.com/RobTillaart/XMLWriter/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/XMLWriter/actions/workflows/jsoncheck.yml)
|
2021-01-29 06:31:58 -05:00
|
|
|
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/XMLWriter/blob/master/LICENSE)
|
|
|
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/XMLWriter.svg?maxAge=3600)](https://github.com/RobTillaart/XMLWriter/releases)
|
|
|
|
|
|
|
|
|
2020-05-25 03:58:04 -04:00
|
|
|
# XMLWriter
|
|
|
|
|
2021-12-29 10:27:03 -05:00
|
|
|
Arduino Library to create simple XML (messages, files, print, ...).
|
2020-05-25 03:58:04 -04:00
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
|
2020-05-25 03:58:04 -04:00
|
|
|
## Description
|
|
|
|
|
|
|
|
The XMLWriter class supports generating XML files and send these over a stream
|
|
|
|
like Ethernet SD.File or Serial.
|
|
|
|
|
|
|
|
When instantiating an XMLWriter one can define the internal buffer size.
|
2020-11-27 05:33:55 -05:00
|
|
|
A bigger buffer will make the output faster, especially for Ethernet and SD.File.
|
|
|
|
The buffer size should be at least 2 bytes and max 250.
|
2021-01-29 06:31:58 -05:00
|
|
|
How much faster depends on the properties of the stream and the platform used.
|
2021-12-29 10:27:03 -05:00
|
|
|
E.g. the baud rate and internal buffer of Serial, packet behaviour of Ethernet,
|
2020-05-25 03:58:04 -04:00
|
|
|
or paging of SD cards.
|
2021-12-29 10:27:03 -05:00
|
|
|
If performance is low one should do test runs with different sizes for the buffer
|
2020-11-27 05:33:55 -05:00
|
|
|
and choose one that is appropriate.
|
2020-05-25 03:58:04 -04:00
|
|
|
|
|
|
|
Indicative sizes based upon the examples.
|
|
|
|
Run your tests to find your application optimum.
|
|
|
|
|
2022-11-27 06:54:57 -05:00
|
|
|
| STREAM | SIZE |
|
|
|
|
|:-----------|:-----------|
|
|
|
|
| Ethernet | 20-30 |
|
|
|
|
| Serial | 5 |
|
|
|
|
| SD File | 10-16 |
|
2020-05-25 03:58:04 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
**IMPORTANT:** When using buffering you should always call **XML.flush()**
|
|
|
|
at the end of the XML generation. This will flush the last bytes in the internal buffer into the output stream.
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
### Constructor
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
- **XMLWriter(Print\* stream = &Serial, uint8_t bufferSize = 10)** Constructor defines the stream and the buffer size
|
2020-11-27 05:33:55 -05:00
|
|
|
to optimize performance vs memory usage.
|
2021-11-11 14:36:58 -05:00
|
|
|
Note the default bufferSize of 10 can be optimized.
|
|
|
|
See table in description above.
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
### Functions for manual layout control
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
- **void setIndentSize(uint8_t size = 2)** preferred a multiple of 2; no limit.
|
|
|
|
- **uint8_t getIndentSize()** returns set indent.
|
|
|
|
- **void incrIndent()** increments indent by 2 spaces.
|
|
|
|
- **void decrIndent()** decrements indent by 2 spaces.
|
|
|
|
- **void indent()** manually indent output.
|
|
|
|
- **void raw(char\* str)** inject any string.
|
2020-11-27 05:33:55 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
### General settings
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
- **void setConfig(uint8_t config)** used to show/strip comment, indent, newLine.
|
|
|
|
use **setConfig(0);** to minimize the output.
|
|
|
|
- **void newLine(uint8_t n)** add a number of newlines to the output, default = 1.
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
### Functions
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
- **void header()** injects standard XML header string, must be first line.
|
|
|
|
- **void reset()** resets internal state, to be called before new XML is written.
|
|
|
|
- **void comment(char\* text, bool multiLine = false)** \<!-- text --\>
|
2020-11-27 05:33:55 -05:00
|
|
|
if multiline == true it does not indent to allow bigger text blocks
|
2021-01-29 06:31:58 -05:00
|
|
|
multiline is default false.
|
2021-11-11 14:36:58 -05:00
|
|
|
- **void flush()** call flush() at the end of writing to empty the internal buffer. **!!**
|
2020-11-27 05:33:55 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
### Functions to create simple tags with named fields
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
- **void tagOpen(char\* tag, bool newLine)** \<tag\>
|
|
|
|
- **void tagOpen(char\* tag, char\* name, bool newLine)** \<tag name="name"\>
|
|
|
|
- **void tagClose()** \</tag\>
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
### Functions to make up tags with multiple fields
|
2020-11-27 05:33:55 -05:00
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
- **void tagStart(char\* tag)** \<tag
|
|
|
|
- **void tagField(char\* field, char\* string)** field="string"
|
|
|
|
- **void tagField(char\* field, T value, uint8_t base = DEC)** standard math types, field="value"
|
|
|
|
- **void tagEnd(bool newline = true, bool addSlash = true);** /\>
|
2020-11-27 05:33:55 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
### Functions to make a node
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
- **void writeNode(char\* tag, bool value);** \<tag\>value\</tag\>
|
|
|
|
- **void writeNode(char\* tag, T value, uint8_t base = DEC);** standard math types.
|
2020-11-27 05:33:55 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
### Helper
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
- **void escape(char\* str)** expands the XML chars: \"\'\<\>\&
|
|
|
|
Note one need to set the **XMLWRITER_ESCAPE_SUPPORT** flag.
|
2020-11-27 05:33:55 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
### Metrics and debug
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
To optimize buffer size in combination with timing.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
- **uint8_t bufferIndex()** returns the size of the internal buffer.
|
|
|
|
- **uint32_t bytesWritten()** idem, since reset().
|
|
|
|
- **void version()** injects the **XMLWRITER_VERSION** as comment in output stream.
|
|
|
|
- **void debug()** injects comment with internal info.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
## Print interface
|
|
|
|
|
|
|
|
XMLWriter 0.2.4 implements the Print interface, so at any moment one can use
|
|
|
|
**print()** or **println()** to inject specific information.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
Note that **tagField()** and **writeNode()** do not support 64 bit integer
|
2020-11-27 05:33:55 -05:00
|
|
|
types and large values of double.
|
|
|
|
My **printHelpers library** helps to convert these to strings which can be printed.
|
|
|
|
See example.
|
|
|
|
|
|
|
|
The Print interface can also be used to print objects that
|
|
|
|
implement the **Printable** interface. See example.
|
|
|
|
|
|
|
|
With the support of the Print interface, **raw()** is becoming obsolete as it only
|
|
|
|
can inject strings.
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
## Configuration flags
|
|
|
|
|
2022-11-27 06:54:57 -05:00
|
|
|
| Flag | Value | Description |
|
|
|
|
|:--------------------|:--------|:--------------------|
|
|
|
|
| XMLWRITER_NONE | 0x00 | minimize output, smaller & faster |
|
|
|
|
| XMLWRITER_COMMENT | 0x01 | allow comments |
|
|
|
|
| XMLWRITER_INDENT | 0x02 | allow indentation |
|
|
|
|
| XMLWRITER_NEWLINE | 0x04 | allow newlines |
|
2021-12-29 10:27:03 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
- **setConfig(XMLWRITER_NONE);** to minimize the output in bytes.
|
|
|
|
- **setConfig(XMLWRITER_NEWLINE);** to break an XML stream in lines.
|
|
|
|
- **setConfig(XMLWRITER_NEWLINE | XMLWRITER_INDENT);** to see XML structure.
|
|
|
|
- **setConfig(XMLWRITER_NEWLINE | XMLWRITER_INDENT | XMLWRITER_COMMENT);** to see XML structure + comments.
|
2020-05-25 03:58:04 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-05-25 03:58:04 -04:00
|
|
|
## Operation
|
|
|
|
|
|
|
|
See examples
|
|
|
|
|
2021-11-11 14:36:58 -05:00
|
|
|
|
|
|
|
## Future
|
|
|
|
|
2022-11-27 06:54:57 -05:00
|
|
|
#### must
|
2021-11-11 14:36:58 -05:00
|
|
|
- update documentation
|
2022-11-27 06:54:57 -05:00
|
|
|
|
|
|
|
#### should
|
|
|
|
|
|
|
|
#### could
|
|
|
|
- move code to .cpp
|
2021-12-29 10:27:03 -05:00
|
|
|
|
|
|
|
|