XML::Bare(3pm) | User Contributed Perl Documentation | XML::Bare(3pm) |
NAME¶
XML::Bare - Minimal XML parser implemented via a C state engineVERSION¶
version 0.47SYNOPSIS¶
use XML::Bare; my $ob = new XML::Bare( text => '<xml><name>Bob</name></xml>' ); # Parse the xml into a hash tree my $root = $ob->parse(); # Print the content of the name node print $root->{xml}->{name}->{value}; # -------------------------------------------------------------- # Load xml from a file ( assume same contents as first example ) my $ob2 = new XML::Bare( file => 'test.xml' ); my $root2 = $ob2->parse(); $root2->{xml}->{name}->{value} = 'Tim'; # Save the changes back to the file $ob2->save(); # -------------------------------------------------------------- # Load xml and verify against XBS ( XML Bare Schema ) my $xml_text = '<xml><item name=bob/></xml>'; my $schema_text = '<xml><item* name=[a-z]+></item*></xml>'; my $ob3 = new XML::Bare( text => $xml_text, schema => { text => $schema_text } ); $ob3->parse(); # this will error out if schema is invalid
DESCRIPTION¶
This module is a 'Bare' XML parser. It is implemented in C. The parser itself is a simple state engine that is less than 500 lines of C. The parser builds a C struct tree from input text. That C struct tree is converted to a Perl hash by a Perl function that makes basic calls back to the C to go through the nodes sequentially. The parser itself will only cease parsing if it encounters tags that are not closed properly. All other inputs will parse, even invalid inputs. To allowing checking for validity, a schema checker is included in the module as well. The schema format is custom and is meant to be as simple as possible. It is based loosely around the way multiplicity is handled in Perl regular expressions.Supported XML¶
To demonstrate what sort of XML is supported, consider the following examples. Each of the PERL statements evaluates to true.- •
- Node containing just text
XML: <xml>blah</xml> PERL: $root->{xml}->{value} eq "blah";
- •
- Subset nodes
XML: <xml><name>Bob</name></xml> PERL: $root->{xml}->{name}->{value} eq "Bob";
- •
- Attributes unquoted
XML: <xml><a href=index.htm>Link</a></xml> PERL: $root->{xml}->{a}->{href}->{value} eq "index.htm";
- •
- Attributes quoted
XML: <xml><a href="index.htm">Link</a></xml> PERL: $root->{xml}->{a}->{href}->{value} eq "index.htm";
- •
- CDATA nodes
XML: <xml><raw><![CDATA[some raw $~<!bad xml<>]]></raw></xml> PERL: $root->{xml}->{raw}->{value} eq "some raw \$~<!bad xml<>";
- •
- Multiple nodes; form array
XML: <xml><item>1</item><item>2</item></xml> PERL: $root->{xml}->{item}->[0]->{value} eq "1";
- •
- Forcing array creation
XML: <xml><multi_item/><item>1</item></xml> PERL: $root->{xml}->{item}->[0]->{value} eq "1";
- •
- One comment supported per node
XML: <xml><!--test--></xml> PERL: $root->{xml}->{comment} eq 'test';
Schema Checking¶
Schema checking is done by providing the module with an XBS (XML::Bare Schema) to check the XML against. If the XML checks as valid against the schema, parsing will continue as normal. If the XML is invalid, the parse function will die, providing information about the failure. The following information is provided in the error message:- •
- The type of error
- •
- Where the error occurred ( line and char )
- •
- A short snippet of the XML at the point of failure
XBS ( XML::Bare Schema ) Format¶
- •
- Required nodes
XML: <xml></xml> XBS: <xml/>
- •
- Optional nodes - allow one
XML: <xml></xml> XBS: <xml item?/> or XBS: <xml><item?/></xml>
- •
- Optional nodes - allow 0 or more
XML: <xml><item/></xml> XBS: <xml item*/>
- •
- Required nodes - allow 1 or more
XML: <xml><item/><item/></xml> XBS: <xml item+/>
- •
- Nodes - specified minimum and maximum number
XML: <xml><item/><item/></xml> XBS: <xml item{1,2}/> or XBS: <xml><item{1,2}/></xml> or XBS: <xml><item{1,2}></item{1,2}></xml>
- •
- Multiple acceptable node formats
XML: <xml><item type=box volume=20/><item type=line length=10/></xml> XBS: <xml><item type=box volume/><item type=line length/></xml>
- •
- Regular expressions checking for values
XML: <xml name=Bob dir=up num=10/> XBS: <xml name=[A-Za-z]+ dir=up|down num=[0-9]+/>
- •
- Require multi_ tags
XML: <xml><multi_item/></xml> XBS: <xml item@/>
Parsed Hash Structure¶
The hash structure returned from XML parsing is created in a specific format. Besides as described above, the structure contains some additional nodes in order to preserve information that will allow that structure to be correctly converted back to XML. Nodes may contain the following 3 additional subnodes:- •
- _i
- •
- _pos
- •
- _att
Parsing Limitations / Features¶
- •
- CDATA parsed correctly, but stripped if unneeded
- •
- Standard XML quoted characters are decoded
- •
- Node position stored, but hash remains unsorted
- •
- Comments are parsed but only one is stored per node.
- •
- Comments override output of immediate value
- •
- PI sections are parsed, but discarded
- •
- Unknown "<!" sections are parsed, but discarded
- •
- Attributes may use no quotes, single quotes, quotes
- •
- Quoted attributes cannot contain escaped quotes
- •
- Attributes are always written back to XML with quotes
- •
- Nodes cannot contain subnodes as well as an immediate value
<node>text<subnode/>text2</node> ( the value of node is text ) <node><subnode/>text</node> ( the value of node is text ) <node> <subnode/>text </node> ( the value of node is "\n " )
Module Functions¶
- •
- "$ob = new XML::Bare( text => "[some
xml]" )"
- •
- "$object = new XML::Bare( file =>
"[filename]" )"
- •
- "$object = new XML::Bare( text => "[some
xml]", file => "[filename]" )"
- •
- "$object = new XML::Bare( file =>
"data.xml", scheme => { file => "scheme.xbs" }
)"
- •
- "$tree = $object->parse()"
- •
- "$tree = $object->simple()"
- •
- "$tree = xmlin( $xmlext, keeproot => 1 )"
- •
- "$text = $object->xml( [root] )"
- •
- "$text = $object->html( [root], [root node name]
)"
- •
- "$object->save()"
- •
- "$value = xval $node, $default"
- •
- "( $name, $age ) = xget( $personnode, qw/name age/
)"
- •
- "$text = XML::Bare::clean( text => "[some
xml]" )"
- •
- "$text = XML::Bare::clean( file =>
"[filename]" )"
- •
- "XML::Bare::clean( file => "[filename]",
save => 1 )"
- •
- "XML::Bare::clean( text => "[some xml]",
save => "[filename]" )"
- •
- "XML::Bare::clean( file => "[filename1]",
save => "[filename2]" )"
- •
- "$html = XML::Bare::tohtml( text => "[some
xml]", root => 'xml' )"
- •
- "$object->add_node( [node], [nodeset name], name
=> value, name2 => value2, ... )"
Example: $object->add_node( $root->{xml}, 'item', name => 'Bob' ); Result: <xml> <item> <name>Bob</name> </item> </xml>
- •
- "$object->add_node_after( [node], [subnode within node to add after], [nodeset name], ... )"
- •
- "$object->del_node( [node], [nodeset name], name
=> value )"
Example: Starting XML: <xml> <a> <b>1</b> </a> <a> <b>2</b> </a> </xml> Code: $xml->del_node( $root->{xml}, 'a', b=>'1' ); Ending XML: <xml> <a> <b>2</b> </a> </xml>
- •
- "$object->find_node( [node], [nodeset name], name
=> value )"
Example: Starting XML: <xml> <ob> <key>1</key> <val>a</val> </ob> <ob> <key>2</key> <val>b</val> </ob> </xml> Code: $object->find_node( $root->{xml}, 'ob', key => '1' )->{val}->{value} = 'test'; Ending XML: <xml> <ob> <key>1</key> <val>test</val> </ob> <ob> <key>2</key> <val>b</val> </ob> </xml>
- •
- "$object->find_by_perl( [nodeset], "[perl
code]" )"
Example: Starting XML: <xml> <ob> <key>1</key> <val>a</val> </ob> <ob> <key>2</key> <val>b</val> </ob> </xml> Code: $object->find_by_perl( $root->{xml}->{ob}, "-key eq '1'" )->[0]->{val}->{value} = 'test'; Ending XML: <xml> <ob> <key>1</key> <val>test</val> </ob> <ob> <key>2</key> <val>b</val> </ob> </xml>
- •
- "XML::Bare::merge( [nodeset1], [nodeset2], [id node
name] )"
Code: my $ob1 = new XML::Bare( text => " <xml> <multi_a/> <a>bob</a> <a> <id>1</id> <color>blue</color> </a> </xml>" ); my $ob2 = new XML::Bare( text => " <xml> <multi_a/> <a>john</a> <a> <id>1</id> <name>bob</name> <bob>1</bob> </a> </xml>" ); my $root1 = $ob1->parse(); my $root2 = $ob2->parse(); merge( $root1->{'xml'}->{'a'}, $root2->{'xml'}->{'a'}, 'id' ); print $ob1->xml( $root1 ); Output: <xml> <multi_a></multi_a> <a>bob</a> <a> <id>1</id> <color>blue</color> <name>bob</name> <bob>1</bob> </a> </xml>
- •
- "XML::Bare::del_by_perl( ... )"
- •
- "XML::Bare::forcearray( [noderef] )"
- •
- "XML::Bare::new_node( ... )"
- •
- "XML::Bare::newhash( ... )"
- •
- "XML::Bare::simplify( [noderef] )"
Functions Used Internally¶
- •
- "check() checkone() readxbs() free_tree_c()"
- •
- "lineinfo() c_parse() c_parsefile() free_tree() xml2obj()"
- •
- "obj2xml() get_root() obj2html() xml2obj_simple()"
Performance¶
In comparison to other available perl xml parsers that create trees, XML::Bare is extremely fast. In order to measure the performance of loading and parsing compared to the alternatives, a templated speed comparison mechanism has been created and included with XML::Bare. The include makebench.pl file runs when you make the module and creates perl files within the bench directory corresponding to the .tmpl contained there. Currently there are three types of modules that can be tested against, executable parsers ( exe.tmpl ), tree parsers ( tree.tmpl ), and parsers that do not generated trees ( notree.tmpl ). A full list of modules currently tested against is as follows:Tiny XML (exe) EzXML (exe) XMLIO (exe) XML::LibXML (notree) XML::Parser (notree) XML::Parser::Expat (notree) XML::Descent (notree) XML::Parser::EasyTree XML::Handler::Trees XML::Twig XML::Smart XML::Simple using XML::Parser XML::Simple using XML::SAX::PurePerl XML::Simple using XML::LibXML::SAX::Parser XML::Simple using XML::Bare::SAX::Parser XML::TreePP XML::Trivial XML::SAX::Simple XML::Grove::Builder XML::XPath::XMLParser XML::DOMTo run the comparisons, run the appropriate perl file within the bench directory. ( exe.pl, tree.pl, or notree.pl ) The script measures the milliseconds of loading and parsing, and compares the time against the time of XML::Bare. So a 7 means it takes 7 times as long as XML::Bare. Here is a combined table of the script run against each alternative using the included test.xml:
-Module- load parse total XML::Bare 1 1 1 XML::TreePP 2.3063 33.1776 6.1598 XML::Parser::EasyTree 4.9405 25.7278 7.4571 XML::Handler::Trees 7.2303 26.5688 9.6447 XML::Trivial 5.0636 12.4715 7.3046 XML::Smart 6.8138 78.7939 15.8296 XML::Simple (XML::Parser) 2.3346 50.4772 10.7455 XML::Simple (PurePerl) 2.361 261.4571 33.6524 XML::Simple (LibXML) 2.3187 163.7501 23.1816 XML::Simple (XML::Bare) 2.3252 59.1254 10.9163 XML::SAX::Simple 8.7792 170.7313 28.3634 XML::Twig 27.8266 56.4476 31.3594 XML::Grove::Builder 7.1267 26.1672 9.4064 XML::XPath::XMLParser 9.7783 35.5486 13.0002 XML::LibXML (notree) 11.0038 4.5758 10.6881 XML::Parser (notree) 4.4698 17.6448 5.8609 XML::Parser::Expat(notree) 3.7681 50.0382 6.0069 XML::Descent (notree) 6.0525 37.0265 11.0322 Tiny XML (exe) 1.0095 EzXML (exe) 1.1284 XMLIO (exe) 1.0165Here is a combined table of the script run against each alternative using the included feed2.xml:
-Module- load parse total XML::Bare 1 1 1 XML::TreePP 2.3068 23.7554 7.6921 XML::Parser::EasyTree 4.8799 25.3691 9.6257 XML::Handler::Trees 6.8545 33.1007 13.0575 XML::Trivial 5.0105 32.0043 11.4113 XML::Simple (XML::Parser) 2.3498 41.9007 12.3062 XML::Simple (PurePerl) 2.3551 224.3027 51.7832 XML::Simple (LibXML) 2.3617 88.8741 23.215 XML::Simple (XML::Bare) 2.4319 37.7355 10.2343 XML::Simple 2.7168 90.7203 26.7525 XML::SAX::Simple 8.7386 94.8276 29.2166 XML::Twig 28.3206 48.1014 33.1222 XML::Grove::Builder 7.2021 30.7926 12.9334 XML::XPath::XMLParser 9.6869 43.5032 17.4941 XML::LibXML (notree) 11.0023 5.022 10.5214 XML::Parser (notree) 4.3748 25.0213 5.9803 XML::Parser::Expat(notree) 3.6555 51.6426 7.4316 XML::Descent (notree) 5.9206 155.0289 18.7767 Tiny XML (exe) 1.2212 EzXML (exe) 1.3618 XMLIO (exe) 1.0145These results show that XML::Bare is, at least on the test machine, running all tests within cygwin, faster at loading and parsing than everything being tested against. The following things are shown as well:
- XML::Bare can parse XML and create a hash tree
in less time than it takes LibXML just to parse.
- XML::Bare can parse XML and create a tree
in less time than all three binary parsers take
just to parse. Note that the executable parsers are not perl modules and are timed using dummy programs that just uses the library to load and parse the example files. The executables are not included with this program. Any source modifications used to generate the shown test results can be found in the bench/src directory of the distribution
CONTRIBUTED CODE¶
The XML dequoting code used is taken from XML::Quote by Sergey Skvortsov ( GDSL on CPAN) with very minor modifications.INSTALLATION¶
See perlmodinstall for information and options on installing Perl modules.BUGS AND LIMITATIONS¶
No bugs have been reported. Please report any bugs or feature requests through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=XML-Bare <http://rt.cpan.org/Public/Dist/Display.html?Name=XML-Bare>.AVAILABILITY¶
The project homepage is https://metacpan.org/release/XML-Bare <https://metacpan.org/release/XML-Bare>. The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a CPAN site near you, or see http://search.cpan.org/dist/XML-Bare/ <http://search.cpan.org/dist/XML-Bare/>. The development version lives at http://github.com/nigelm/xml-bare <http://github.com/nigelm/xml-bare> and may be cloned from git://github.com/nigelm/xml-bare.git <git://github.com/nigelm/xml-bare.git>. Instead of sending patches, please fork this project using the standard git and github infrastructure.AUTHORS¶
- •
- David Helkowski <cpan@codechild.com>
- •
- Nigel Metheringham <nigelm@cpan.org>
COPYRIGHT AND LICENSE¶
This software is Copyright (c) 2012 by David Helkowski. This is free software, licensed under:The GNU General Public License, Version 2, June 1991
2012-02-08 | perl v5.14.2 |