|
NAMEXML::TinyXML - Little and efficient Perl module to manage xml data. SYNOPSIS use XML::TinyXML;
# First create an XML Context
$xml = XML::TinyXML->new();
# and add a root node
$xml->addRootNode('nodelabel', 'somevalue', { attr1 => v1, attr2 => v2 });
# if you want to reuse a previously created node object ...
#
# ( you can create one calling :
# * %attrs = ( attr1 => v1, attr2 => v2 );
# * $node = XML::TinyXML::Node->new('nodelabel', param => 'somevalue', attrs => \%attrs);
# )
#
# the new XML Context can be created giving the root node directly to the constructor
$xml = XML::TinyXML->new($node);
######
# A better (and easier) option is also to let the context constructor create the rootnode for you:
$xml = XML::TinyXML->new('rootnode', param => 'somevalue', attrs => { attr1 => v1, attr2 => v2 });
# an empty root node can be created as well:
$xml = XML::TinyXML->new('rootnode');
# You can later obtain a reference to the node object using the getNode() method
$node = $xml->getNode('/rootnode');
# the leading '/' is optional ... since all paths will be considered absolute and
# first element is assumed to be always a root node
$node = $xml->getNode('rootnode');
# xpath-like predicates are also supported so you can also do something like
$node->addChildNode('child', 'value1');
$node->addChildNode('child', 'value2');
$child2 = $xml->getNode('/rootnode/child[2]');
# or even
$node->addChildNode('child', 'value3', { attr => 'val' });
# the 3rd 'child' has an attribute
$child3 = $xml->getNode('/rootnode/child[@attr="val"]');
#### IMPORTANT NOTE: ####
# this is not xpath syntax. use XML::TinyXML::Selector::XPath
# if you want to use an xpath-compliant syntax
# see XML::TinyXML::Node documentation for further details on possible
# operations on a node reference
######## #########
########## hashref2xml and xml2hashref facilities ###########
######## #########
# An useful facility is loading/dumping of hashrefs from/to xml
# for ex:
$hashref = { some => 'thing', someother => 'thing' };
my $xml = XML::TinyXML->new($hashref, param => 'mystruct');
# or to load on an existing XML::TinyXML object
$xml->loadHash($hashref, 'mystruct');
# we can also create and dump to string all at once :
my $xmlstring = XML::TinyXML->new($hashref, param => 'mystruct')->dump;
# to reload the hashref back
my $hashref = $xml->toHash;
DESCRIPTIONSince in some environments it could be desirable to avoid installing Expat, XmlParser and blahblahblah, needed by most XML-related perl modules, my main scope was to obtain a fast xml library usable from perl (so with the possibility to expose a powerful api) but without the need to install a lot of other modules (or even C libraries) to have it working. The actual version of XML::TinyXML (0.18 when I'm writing this) implements an xpath selector and supports encodings (through iconv). The OO Tree-based api allows to : - create a new document programmaticaly
- import an existing document from a file
- import an existing document by parsing a memory buffer
- modify the loaded/created document programmatically
- export the document to an xml file
- bidirectional conversion between xml documents and perl hashrefs
There are other "no-dependencies" tiny xml implementations on CPAN. Notably : XML::Easy and XML::Tiny but both are still missing some key features which are actually implemented in this module and are required by the projects where I use it, in particular : - an OO api to allow changing and re-exporting the xml document,
- an easy-to-use bidirectional xml<->hashref conversion
- encoding-conversion capabilities
- xpath selectors
- small memory footprint (well... it's always a perl module)
The underlying xml implementation resides in txml.c and is imported in the perl context through XS. It uses a linkedlist implementation took out of freebsd kernel and is supposed to be fast enough to represent and access 'not-huge' xml trees. If at some stage there will be need for more preformance in accessing the xml data (especially when using the xpath selector) an hash-table based index could be built on top of the tree structure to speed-up access to inner parts of the tree when using paths. An Event-based api is actually missing, but will be provided in future releases. METHODS
EXPORTNone by default. Exportable constantsXML_BADARGS XML_GENERIC_ERR XML_LINKLIST_ERR XML_MEMORY_ERR XML_NOERR XML_OPEN_FILE_ERR XML_PARSER_GENERIC_ERR XML_UPDATE_ERR XML_BAD_CHARS XML_MROOT_ERR XML_NODETYPE_SIMPLE XML_NODETYPE_COMMENT XML_NODETYPE_CDATA Exportable C functionsTXml *XmlCreateContext() void XmlDestroyContext(TXml *xml) XmlNode *XmlCreateNode(char *name,char *val,XmlNode *parent); char *XmlGetNodeValue(XmlNode *node); XmlErr XmlSetNodeValue(XmlNode *node,char *val); void XmlDestroyNode(XmlNode *node); int XmlAddAttribute(XmlNode *node, char *name, char *val) int XmlAddRootNode(TXml *xml, XmlNode *node) int XmlRemoveNode(TXml *xml,char *path); void XmlRemoveChildNodeAtIndex(XmlNode *node, unsigned long index); void XmlRemoveChildNode(XmlNode *parent, XmlNode *child); XmlErr XmlAddChildNode(XmlNode *parent,XmlNode *child); XmlNode *XmlGetChildNode(XmlNode *node, unsigned long index) XmlNode *XmlGetChildNodeByName(XmlNode *node,char *name); XmlNode *XmlGetNode(TXml *xml, char *path) unsigned long XmlCountBranches(TXml *xml) int XmlRemoveBranch(TXml *xml, unsigned long index) XmlNode *XmlGetBranch(TXml *xml,unsigned long index); int XmlSubstBranch(TXml *xml,unsigned long index, XmlNode *newBranch); int XmlParseBuffer(TXml *xml, char *buf) int XmlSave(TXml *xml, char *path) char *XmlDump(TXml *xml, int *outlen) SEE ALSOXML::TinyXML::Node You should also see libtinyxml documentation (mostly txml.h, redistributed with this module) If you need a more complete and featureful XML implementation try looking at XML::LibXML XML::Parser XML::API XML::XPath or other modules based on either expat or libxml2 AUTHORxant, <xant@cpan.org> COPYRIGHT AND LICENSECopyright (C) 2008-2010 by xant This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
|