2
XML::Bare - Minimal XML parser implemented via a C state engine
10
my $ob = new XML::Bare( text => '<xml><name>Bob</name></xml>' );
12
# Parse the xml into a hash tree
13
my $root = $ob->parse();
15
# Print the content of the name node
16
print $root->{xml}->{name}->{value};
18
# --------------------------------------------------------------
20
# Load xml from a file ( assume same contents as first example )
21
my $ob2 = new XML::Bare( file => 'test.xml' );
23
my $root2 = $ob2->parse();
25
$root2->{xml}->{name}->{value} = 'Tim';
27
# Save the changes back to the file
30
# --------------------------------------------------------------
32
# Load xml and verify against XBS ( XML Bare Schema )
33
my $xml_text = '<xml><item name=bob/></xml>';
34
my $schema_text = '<xml><item* name=[a-z]+></item*></xml>';
35
my $ob3 = new XML::Bare( text => $xml_text, schema => { text => $schema_text } );
36
$ob3->parse(); # this will error out if schema is invalid
39
This module is a 'Bare' XML parser. It is implemented in C. The parser
40
itself is a simple state engine that is less than 500 lines of C. The
41
parser builds a C struct tree from input text. That C struct tree is
42
converted to a Perl hash by a Perl function that makes basic calls back
43
to the C to go through the nodes sequentially.
45
The parser itself will only cease parsing if it encounters tags that are
46
not closed properly. All other inputs will parse, even invalid inputs.
47
To allowing checking for validity, a schema checker is included in the
50
The schema format is custom and is meant to be as simple as possible. It
51
is based loosely around the way multiplicity is handled in Perl regular
55
To demonstrate what sort of XML is supported, consider the following
56
examples. Each of the PERL statements evaluates to true.
58
* Node containing just text
61
PERL: $root->{xml}->{value} eq "blah";
65
XML: <xml><name>Bob</name></xml>
66
PERL: $root->{xml}->{name}->{value} eq "Bob";
70
XML: <xml><a href=index.htm>Link</a></xml>
71
PERL: $root->{xml}->{a}->{href}->{value} eq "index.htm";
75
XML: <xml><a href="index.htm">Link</a></xml>
76
PERL: $root->{xml}->{a}->{href}->{value} eq "index.htm";
80
XML: <xml><raw><![CDATA[some raw $~<!bad xml<>]]></raw></xml>
81
PERL: $root->{xml}->{raw}->{value} eq "some raw \$~<!bad xml<>";
83
* Multiple nodes; form array
85
XML: <xml><item>1</item><item>2</item></xml>
86
PERL: $root->{xml}->{item}->[0]->{value} eq "1";
88
* Forcing array creation
90
XML: <xml><multi_item/><item>1</item></xml>
91
PERL: $root->{xml}->{item}->[0]->{value} eq "1";
93
* One comment supported per node
95
XML: <xml><!--test--></xml>
96
PERL: $root->{xml}->{comment} eq 'test';
99
Schema checking is done by providing the module with an XBS (XML::Bare
100
Schema) to check the XML against. If the XML checks as valid against the
101
schema, parsing will continue as normal. If the XML is invalid, the
102
parse function will die, providing information about the failure.
104
The following information is provided in the error message:
108
* Where the error occurred ( line and char )
110
* A short snippet of the XML at the point of failure
112
XBS ( XML::Bare Schema ) Format
118
* Optional nodes - allow one
122
or XBS: <xml><item?/></xml>
124
* Optional nodes - allow 0 or more
126
XML: <xml><item/></xml>
129
* Required nodes - allow 1 or more
131
XML: <xml><item/><item/></xml>
134
* Nodes - specified minimum and maximum number
136
XML: <xml><item/><item/></xml>
137
XBS: <xml item{1,2}/>
138
or XBS: <xml><item{1,2}/></xml>
139
or XBS: <xml><item{1,2}></item{1,2}></xml>
141
* Multiple acceptable node formats
143
XML: <xml><item type=box volume=20/><item type=line length=10/></xml>
144
XBS: <xml><item type=box volume/><item type=line length/></xml>
146
* Regular expressions checking for values
148
XML: <xml name=Bob dir=up num=10/>
149
XBS: <xml name=[A-Za-z]+ dir=up|down num=[0-9]+/>
151
* Require multi_ tags
153
XML: <xml><multi_item/></xml>
156
Parsed Hash Structure
157
The hash structure returned from XML parsing is created in a specific
158
format. Besides as described above, the structure contains some
159
additional nodes in order to preserve information that will allow that
160
structure to be correctly converted back to XML.
162
Nodes may contain the following 3 additional subnodes:
166
The character offset within the original parsed XML of where the node
167
begins. This is used to provide line information for errors when XML
168
fails a schema check.
172
This is a number indicating the ordering of nodes. It is used to allow
173
items in a perl hash to be sorted when writing back to xml. Note that
174
items are not sorted after parsing in order to save time if all you
175
are doing is reading and you do not care about the order.
177
In future versions of this module an option will be added to allow you
178
to sort your nodes so that you can read them in order. ( note that
179
multiple nodes of the same name are stored in order )
183
This is a boolean value that exists and is 1 iff the node is an
186
Parsing Limitations / Features
187
* CDATA parsed correctly, but stripped if unneeded
189
Currently the contents of a node that are CDATA are read and put into
190
the value hash, but the hash structure does not have a value
191
indicating the node contains CDATA.
193
When converting back to XML, the contents of the value hash are parsed
194
to check for xml incompatible data using a regular expression. If
195
'CDATA like' stuff is encountered, the node is output as CDATA.
197
* Standard XML quoted characters are decoded
199
The basic XML quoted characters - "&" ">" "<" "quot;" and
200
"'" - are recognised and decoded when reading values. However
201
when writing the builder will put any values that need quoting into a
202
CDATA wrapper as described above.
204
* Node position stored, but hash remains unsorted
206
The ordering of nodes is noted using the '_pos' value, but the hash
207
itself is not ordered after parsing. Currently items will be out of
208
order when looking at them in the hash.
210
Note that when converted back to XML, the nodes are then sorted and
211
output in the correct order to XML. Note that nodes of the same name
212
with the same parent will be grouped together; the position of the
213
first item to appear will determine the output position of the group.
215
* Comments are parsed but only one is stored per node.
217
For each node, there can be a comment within it, and that comment will
218
be saved and output back when dumping to XML.
220
* Comments override output of immediate value
222
If a node contains only a comment node and a text value, only the
223
comment node will be displayed. This is in line with treating a
224
comment node as a node and only displaying immediate values when a
225
node contains no subnodes.
227
* PI sections are parsed, but discarded
229
* Unknown "<!" sections are parsed, but discarded
231
* Attributes may use no quotes, single quotes, quotes
233
* Quoted attributes cannot contain escaped quotes
235
No escape character is recognized within quotes. As a result, regular
236
quotes cannot be stored to XML, or the written XML will not be
237
correct, due to all attributes always being written using quotes.
239
* Attributes are always written back to XML with quotes
241
* Nodes cannot contain subnodes as well as an immediate value
243
Actually nodes can in fact contain a value as well, but that value
244
will be discarded if you write back to XML. That value is equal to the
245
first continuous string of text besides a subnode.
247
<node>text<subnode/>text2</node>
248
( the value of node is text )
250
<node><subnode/>text</node>
251
( the value of node is text )
256
( the value of node is "\n " )
259
* "$ob = new XML::Bare( text => "[some xml]" )"
261
Create a new XML object, with the given text as the xml source.
263
* "$object = new XML::Bare( file => "[filename]" )"
265
Create a new XML object, with the given filename/path as the xml
268
* "$object = new XML::Bare( text => "[some xml]", file => "[filename]"
271
Create a new XML object, with the given text as the xml input, and the
272
given filename/path as the potential output ( used by save() )
274
* "$object = new XML::Bare( file => "data.xml", scheme => { file =>
277
Create a new XML object and check to ensure it is valid xml by way of
280
* "$tree = $object->parse()"
282
Parse the xml of the object and return a tree reference
284
* "$tree = $object->simple()"
286
Alternate to the parse function which generates a tree similar to that
287
generated by XML::Simple. Note that the sets of nodes are turned into
288
arrays always, regardless of whether they have a 'name' attribute,
291
Note that currently the generated tree cannot be used with any of the
292
functions in this module that operate upon trees. The function is
293
provided purely as a quick and dirty way to read simple XML files.
295
* "$tree = xmlin( $xmlext, keeproot => 1 )"
297
The xmlin function is a shortcut to creating an XML::Bare object and
298
parsing it using the simple function. It behaves similarly to the
299
XML::Simple function by the same name. The keeproot option is optional
300
and if left out the root node will be discarded, same as the function
303
* "$text = $object->xml( [root] )"
305
Take the hash tree in [root] and turn it into cleanly indented ( 2
308
* "$text = $object->html( [root], [root node name] )"
310
Take the hash tree in [root] and turn it into nicely colorized and
311
styled html. [root node name] is optional.
315
The the current tree in the object, cleanly indent it, and save it to
316
the file parameter specified when creating the object.
318
* "$value = xval $node, $default"
320
Returns the value of $node or $default if the node does not exist. If
321
default is not passed to the function, then '' is returned as a
322
default value when the node does not exist.
324
* "( $name, $age ) = xget( $personnode, qw/name age/ )"
326
Shortcut function to grab a number of values from a node all at the
327
same time. Note that this function assumes that all of the subnodes
328
exist; it will fail if they do not.
330
* "$text = XML::Bare::clean( text => "[some xml]" )"
332
Shortcut to creating an xml object and immediately turning it into
335
* "$text = XML::Bare::clean( file => "[filename]" )"
339
* "XML::Bare::clean( file => "[filename]", save => 1 )"
341
Clean up the xml in the file, saving the results back to the file
343
* "XML::Bare::clean( text => "[some xml]", save => "[filename]" )"
345
Clean up the xml provided, and save it into the specified file.
347
* "XML::Bare::clean( file => "[filename1]", save => "[filename2]" )"
349
Clean up the xml in filename1 and save the results to filename2.
351
* "$html = XML::Bare::tohtml( text => "[some xml]", root => 'xml' )"
353
Shortcut to creating an xml object and immediately turning it into
354
html. Root is optional, and specifies the name of the root node for
355
the xml ( which defaults to 'xml' )
357
* "$object->add_node( [node], [nodeset name], name => value, name2 =>
361
$object->add_node( $root->{xml}, 'item', name => 'Bob' );
370
* "$object->add_node_after( [node], [subnode within node to add after],
371
[nodeset name], ... )"
373
* "$object->del_node( [node], [nodeset name], name => value )"
387
$xml->del_node( $root->{xml}, 'a', b=>'1' );
396
* "$object->find_node( [node], [nodeset name], name => value )"
412
$object->find_node( $root->{xml}, 'ob', key => '1' )->{val}->{value} = 'test';
426
* "$object->find_by_perl( [nodeset], "[perl code]" )"
428
find_by_perl evaluates some perl code for each node in a set of nodes,
429
and returns the nodes where the perl code evaluates as true. In order
430
to easily reference node values, node values can be directly referred
431
to from within the perl code by the name of the node with a dash(-) in
432
front of the name. See the example below.
434
Note that this function returns an array reference as opposed to a
435
single node unlike the find_node function.
451
$object->find_by_perl( $root->{xml}->{ob}, "-key eq '1'" )->[0]->{val}->{value} = 'test';
465
* "XML::Bare::merge( [nodeset1], [nodeset2], [id node name] )"
467
Merges the nodes from nodeset2 into nodeset1, matching the contents of
468
each node based up the content in the id node.
473
my $ob1 = new XML::Bare( text => "
482
my $ob2 = new XML::Bare( text => "
492
my $root1 = $ob1->parse();
493
my $root2 = $ob2->parse();
494
merge( $root1->{'xml'}->{'a'}, $root2->{'xml'}->{'a'}, 'id' );
495
print $ob1->xml( $root1 );
509
* "XML::Bare::del_by_perl( ... )"
511
Works exactly like find_by_perl, but deletes whatever matches.
513
* "XML::Bare::forcearray( [noderef] )"
515
Turns the node reference into an array reference, whether that node is
516
just a single node, or is already an array reference.
518
* "XML::Bare::new_node( ... )"
520
Creates a new node...
522
* "XML::Bare::newhash( ... )"
524
Creates a new hash with the specified value.
526
* "XML::Bare::simplify( [noderef] )"
528
Take a node with children that have immediate values and creates a
529
hashref to reference those values by the name of each child.
531
Functions Used Internally
532
* "check() checkone() readxbs() free_tree_c()"
534
* "lineinfo() c_parse() c_parsefile() free_tree() xml2obj()"
536
* "obj2xml() get_root() obj2html() xml2obj_simple()"
539
In comparison to other available perl xml parsers that create trees,
540
XML::Bare is extremely fast. In order to measure the performance of
541
loading and parsing compared to the alternatives, a templated speed
542
comparison mechanism has been created and included with XML::Bare.
544
The include makebench.pl file runs when you make the module and creates
545
perl files within the bench directory corresponding to the .tmpl
548
Currently there are three types of modules that can be tested against,
549
executable parsers ( exe.tmpl ), tree parsers ( tree.tmpl ), and parsers
550
that do not generated trees ( notree.tmpl ).
552
A full list of modules currently tested against is as follows:
559
XML::Parser::Expat (notree)
560
XML::Descent (notree)
561
XML::Parser::EasyTree
565
XML::Simple using XML::Parser
566
XML::Simple using XML::SAX::PurePerl
567
XML::Simple using XML::LibXML::SAX::Parser
568
XML::Simple using XML::Bare::SAX::Parser
573
XML::XPath::XMLParser
576
To run the comparisons, run the appropriate perl file within the bench
577
directory. ( exe.pl, tree.pl, or notree.pl )
579
The script measures the milliseconds of loading and parsing, and
580
compares the time against the time of XML::Bare. So a 7 means it takes 7
581
times as long as XML::Bare.
583
Here is a combined table of the script run against each alternative
584
using the included test.xml:
586
-Module- load parse total
588
XML::TreePP 2.3063 33.1776 6.1598
589
XML::Parser::EasyTree 4.9405 25.7278 7.4571
590
XML::Handler::Trees 7.2303 26.5688 9.6447
591
XML::Trivial 5.0636 12.4715 7.3046
592
XML::Smart 6.8138 78.7939 15.8296
593
XML::Simple (XML::Parser) 2.3346 50.4772 10.7455
594
XML::Simple (PurePerl) 2.361 261.4571 33.6524
595
XML::Simple (LibXML) 2.3187 163.7501 23.1816
596
XML::Simple (XML::Bare) 2.3252 59.1254 10.9163
597
XML::SAX::Simple 8.7792 170.7313 28.3634
598
XML::Twig 27.8266 56.4476 31.3594
599
XML::Grove::Builder 7.1267 26.1672 9.4064
600
XML::XPath::XMLParser 9.7783 35.5486 13.0002
601
XML::LibXML (notree) 11.0038 4.5758 10.6881
602
XML::Parser (notree) 4.4698 17.6448 5.8609
603
XML::Parser::Expat(notree) 3.7681 50.0382 6.0069
604
XML::Descent (notree) 6.0525 37.0265 11.0322
605
Tiny XML (exe) 1.0095
609
Here is a combined table of the script run against each alternative
610
using the included feed2.xml:
612
-Module- load parse total
614
XML::TreePP 2.3068 23.7554 7.6921
615
XML::Parser::EasyTree 4.8799 25.3691 9.6257
616
XML::Handler::Trees 6.8545 33.1007 13.0575
617
XML::Trivial 5.0105 32.0043 11.4113
618
XML::Simple (XML::Parser) 2.3498 41.9007 12.3062
619
XML::Simple (PurePerl) 2.3551 224.3027 51.7832
620
XML::Simple (LibXML) 2.3617 88.8741 23.215
621
XML::Simple (XML::Bare) 2.4319 37.7355 10.2343
622
XML::Simple 2.7168 90.7203 26.7525
623
XML::SAX::Simple 8.7386 94.8276 29.2166
624
XML::Twig 28.3206 48.1014 33.1222
625
XML::Grove::Builder 7.2021 30.7926 12.9334
626
XML::XPath::XMLParser 9.6869 43.5032 17.4941
627
XML::LibXML (notree) 11.0023 5.022 10.5214
628
XML::Parser (notree) 4.3748 25.0213 5.9803
629
XML::Parser::Expat(notree) 3.6555 51.6426 7.4316
630
XML::Descent (notree) 5.9206 155.0289 18.7767
631
Tiny XML (exe) 1.2212
635
These results show that XML::Bare is, at least on the test machine,
636
running all tests within cygwin, faster at loading and parsing than
637
everything being tested against.
639
The following things are shown as well: - XML::Bare can parse XML and
640
create a hash tree in less time than it takes LibXML just to parse. -
641
XML::Bare can parse XML and create a tree in less time than all three
642
binary parsers take just to parse.
644
Note that the executable parsers are not perl modules and are timed
645
using dummy programs that just uses the library to load and parse the
646
example files. The executables are not included with this program. Any
647
source modifications used to generate the shown test results can be
648
found in the bench/src directory of the distribution
651
The XML dequoting code used is taken from XML::Quote by *Sergey
652
Skvortsov* (*GDSL* on CPAN) with very minor modifications.
655
See perlmodinstall for information and options on installing Perl
659
No bugs have been reported.
661
Please report any bugs or feature requests through the web interface at
662
<http://rt.cpan.org/Public/Dist/Display.html?Name=XML-Bare>.
665
The project homepage is <https://metacpan.org/release/XML-Bare>.
667
The latest version of this module is available from the Comprehensive
668
Perl Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a
669
CPAN site near you, or see <http://search.cpan.org/dist/XML-Bare/>.
671
The development version lives at <http://github.com/nigelm/xml-bare> and
672
may be cloned from <git://github.com/nigelm/xml-bare.git>. Instead of
673
sending patches, please fork this project using the standard git and
674
github infrastructure.
677
* David Helkowski <cpan@codechild.com>
679
* Nigel Metheringham <nigelm@cpan.org>
681
COPYRIGHT AND LICENSE
682
This software is Copyright (c) 2012 by David Helkowski.
684
This is free software, licensed under:
686
The GNU General Public License, Version 2, June 1991
5
Download XML-Bare-latest.tar.gz and untar it:
6
% tar -xzf XML-Bare-latest.tar.gz
8
Follow the standard steps for making and compiling a perl module:
9
% cd XML-Bare-X.XX ( X.XX = the latest version )
16
Copyright (C) 2013 David Helkowski
18
This program is free software; you can redistribute it and/or
19
modify it under the terms of the GNU General Public License as
20
published by the Free Software Foundation; either version 2 of the
21
License, or (at your option) any later version. You may also can
22
redistribute it and/or modify it under the terms of the Perl
25
This program is distributed in the hope that it will be useful,
26
but WITHOUT ANY WARRANTY; without even the implied warranty of
27
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28
GNU General Public License for more details.