![]() |
libgenua
Basic Geometry, Numerical Algorithms and Interfaces
|
XML Element.
A very simple class for reading and writing XML documents. It is intended for minimum fuss and maximum ease of use and not for good performance with large documents.
Usage example:
XmlElement doc("sml"); doc["version"] = "1.3"; XmlElement fe("client"); fe["name"] = "Oscar"; fe["job"] = "clerk"; fe.text() = "Lousy speaker, avid worker."; doc.append(fe); XmlElement se("client"); se["name"] = "Lily"; se["job"] = "CTO"; doc.append(se); doc.write("clients.sml");
XmlElement uses the expat xml parser written by James Clark jjc@j. clar k.com
TODO : Consider using a flat vector<pair<string,string>> for the the attribute map since there are usually very few of them.
#include <xmlelement.h>
Public Types | |
enum | StorageFormat |
supported on-disk file formats | |
typedef boost::shared_ptr< char[]> | BlobType |
storage for binary data | |
Public Member Functions | |
XmlElement (const std::string &tg="") | |
create an element named tg | |
XmlElement (std::string &&tg) | |
create an element named tg, common case with temporary as tag | |
XmlElement (const std::string &tg, const std::string &content) | |
shortcut constructor for simple string elements | |
XmlElement (XmlElement &&a) | |
move constructor (turns out MSVC doesn't auto-generate this one) | |
XmlElement (const XmlElement &)=default | |
copy constructor | |
XmlElement & | operator= (XmlElement &&a) |
move assignment | |
XmlElement & | operator= (const XmlElement &)=default |
copy assignment | |
bool | empty () const |
an element is empty if there are no children, attributes, nor payload | |
const std::string & | name () const |
access tag | |
void | rename (const std::string &s) |
change tag name | |
bool | hasAttribute (const std::string &key) const |
check if attribute exists | |
const std::string & | attribute (const std::string &key) const |
access attribute | |
const std::string & | attribute (const std::string &key, const std::string &strDefault) const |
access attribute, provide default value | |
attr_iterator | attrBegin () const |
attribute iterator access | |
attr_iterator | attrEnd () const |
attribute iterator access | |
double | attr2float (const std::string &key, double x) const |
if attribute present, convert to float, else return default | |
int | attr2int (const std::string &key, int x) const |
if attribute present, convert to int, else return default | |
bool | attr2bool (const std::string &key, bool x) const |
if attribute present, convert to bool, else return default | |
template<class Streamable > | |
bool | fromAttribute (const std::string &key, Streamable &sth) const |
use std::stringstream to set attrbute only when present | |
std::string & | attribute (const std::string &key) |
access attribute | |
std::string & | operator[] (const std::string &key) |
access attribute | |
const std::string & | text () const |
access text | |
std::string & | text () |
change text | |
void | text (const std::string &s) |
change text | |
void | text (const char *ptr, size_t n) |
change text | |
const_iterator | begin () const |
iterate over children | |
const_iterator | end () const |
iterate over children | |
const_iterator | findChild (const std::string &s) const |
find first child with tag "tg" | |
const XmlElement * | findNode (const std::string &path) const |
find first node matching path, return 0 if not found | |
const XmlElement * | findAnyTag (const std::string &tag) const |
find first element matching tag, using depth-first search | |
mutable_iterator | mbegin () |
iterate over children | |
mutable_iterator | mend () |
iterate over children | |
size_t | append (const XmlElement &c) |
append a child element | |
size_t | append (XmlElement &&c) |
append a child element | |
size_t | replaceAppend (const XmlElement &c) |
replace if already present, else append a child element | |
XmlElement & | append (const std::string &childTag, const std::string &childText=std::string()) |
create a child element with tag and text content, append | |
template<class ElementType > | |
XmlElement & | append (const std::string &childTag, size_t nvalues, const ElementType values[], bool share=false) |
for convenience: Create an labeled child element containing vector data | |
uint | children () const |
count child elements | |
void | eraseChild (uint k) |
erase child element k | |
void | replace (uint k, const XmlElement &xe) |
replace child element | |
template<typename ScalarType > | |
void | asBinary (size_t nval, const ScalarType a[], bool share=false) |
register binary instead of character data payload, copy or link content | |
void | fetch (size_t n, int a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, uint a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, int8_t a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, uint8_t a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, int16_t a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, uint16_t a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, int64_t a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, uint64_t a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, float a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, double a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, std::complex< float > a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
void | fetch (size_t n, std::complex< double > a[]) const |
universal access functions which use binary or text (exception on size mismatch) | |
int | blobType () const |
typecode for binary payload | |
const char * | blobPointer () const |
access pointer to binary payload | |
size_t | blobBytes () const |
number of bytes in blob | |
BlobType | blob () const |
access shared pointer to block of binary data | |
void | shrink () |
shrink members to minimum size | |
void | detach () |
make a deep copy of contained data | |
bool | read (std::istream &in, int format) |
read from stream in specified format | |
bool | write (std::ostream &os, int format) const |
write to stream in specified format | |
void | read (const std::string &fname) |
read from any supported file | |
void | read (ZipFile &zf) |
read element from currently open file in zip archive | |
void | write (const std::string &fname, StorageFormat fmt) const |
write to file in format provided as argument | |
BinFileNodePtr | toGbf (bool share=true) const |
return GBF representation | |
void | fromGbf (const BinFileNodePtr &bfp, bool share=true) |
construct from GBF representation | |
void | zwrite (const std::string &zfile, int compression=1) const |
store into zip file | |
void | zread (const std::string &zfile) |
read from zip file | |
Protected Member Functions | |
void | zwrite (std::ostream &xs, ZipFile &zf, uint indent) const |
write node with binary data | |
void | zread (ZipFile &zf) |
read child with binary data | |
void | xwrite (const std::string &fname) const |
write root element to plain ascii xml file | |
void | xread (std::istream &in) |
read node recursively from plain text file, ignore binary data | |
void | xwrite (std::ostream &os, uint indent=0) const |
write as plain xml text, dump binary to CDATA | |
template<typename Scalar > | |
void | fetchAnything (size_t n, Scalar a[]) const |
extract array from text content | |
void | binaryToText (std::ostream &os) const |
dump binary data to text stream | |
Private Attributes | |
std::string | m_tag |
tag name and enclosed text | |
StringMap | m_attributes |
attribute map | |
ElementList | m_siblings |
children | |
int | m_typecode |
type of binary data included | |
BlobType | m_blob |
storage for binary data | |
size_t | m_nbytes |
number of bytes stored | |