The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

XML::Bare - Minimal XML parser implemented via a C state engine

VERSION

0.30

SYNOPSIS

use XML::Bare;

my $xml = new XML::Bare( text => '<xml><name>Bob</name></xml>' );

# Parse the xml into a hash tree
my $root = $xml->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 $xml2 = new XML::Bare( file => 'test.xml' );

my $root2 = $xml2->parse();

$root2->{xml}->{name}->{value} = 'Tim';

# Save the changes back to the file
$xml2->save();  

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++ class tree from input text. That C++ class 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.

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';

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 2 additional subnodes:

  • _pos

    This is a number indicating the ordering of nodes. It is used to allow items in a perl hash to be sorted when writing back to xml. Note that items are not sorted after parsing in order to save time if all you are doing is reading and you do not care about the order.

    In future versions of this module an option will be added to allow you to sort your nodes so that you can read them in order.

  • att

    This is a boolean value that exists and is 1 iff the node is an attribute.

Parsing Limitations / Features

  • CDATA parsed correctly, but stripped if unneeded

    Currently the contents of a node that are CDATA are read and put into the value hash, but the hash structure does not have a value indicating the node contains CDATA.

    When converting back to XML, the contents are the value hash are parsed to check for xml incompatible data using a regular expression. If 'CDATA like' stuff is encountered, the node is output as CDATA.

  • Node position stored, but hash remains unsorted

    The ordering of nodes is noted using the '_pos' value, but the hash itself is not ordered after parsing. Currently items will be out of order when looking at them in the hash.

    Note that when converted back to XML, the nodes are then sorted and output in the correct order to XML. Note that nodes of the same name with the same parent will be grouped together; the position of the first item to appear will determine the output position of the group.

  • Comments are parsed but only one is stored per node.

    For each node, there can be a comment within it, and that comment will be saved and output back when dumping to XML.

  • Comments override output of immediate value

    If a node contains only a comment node and a text value, only the comment node will be displayed. This is in line with treating a comment node as a node and only displaying immediate values when a node contains no subnodes.

  • 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

    No escape character is recognized within quotes. As a result, regular quotes cannot be stored to XML, or the written XML will not be correct, due to all attributes always being written using quotes.

  • Attributes are always written back to XML with quotes

  • Nodes cannot contain subnodes as well as an immediate value

    Actually nodes can in fact contain a value as well, but that value will be discarded if you write back to XML. That value is equal to the first continuous string of text besides a subnode.

    <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]" )

    Create a new XML object, with the given text as the xml source.

  • $object = new XML::Bare( file => "[filename]" )

    Create a new XML object, with the given filename/path as the xml source

  • $object = new XML::Bare( text => "[some xml]", file => "[filename]" )

    Create a new XML object, with the given text as the xml input, and the given filename/path as the potential output ( used by save() )

  • $tree = $object->parse()

    Parse the xml of the object and return a tree reference

  • $tree = $object->simple()

    Alternate to the parse function which generates a tree similar to that generated by XML::Simple. Note that the sets of nodes are turned into arrays always, regardless of whether they have a 'name' attribute, unlike XML::Simple.

    Note that currently the generated tree cannot be used with any of the functions in this module that operate upon trees. The function is provided purely as a quick and dirty way to read simple XML files.

    Also note that you cannot rely upon this function being contained in future versions of XML::Bare; the function will likely be split off into an optimized version meant purely to operate in this fashion.

  • $text = $object->xml( [root] )

    Take the hash tree in [root] and turn it into cleanly indented ( 2 spaces ) XML text.

  • $object->save()

    The the current tree in the object, cleanly indent it, and save it to the file paramter specified when creating the object.

  • $text = XML::Bare::clean( text => "[some xml]" )

    Shortcut to creating an xml object and immediately turning it into clean xml text.

  • $text = XML::Bare::clean( file => "[filename]" )

    Similar to previous.

  • XML::Bare::clean( file => "[filename]", save => 1 )

    Clean up the xml in the file, saving the results back to the file

  • XML::Bare::clean( text => "[some xml]", save => "[filename]" )

    Clean up the xml provided, and save it into the specified file.

  • XML::Bare::clean( file => "[filename1]", save => "[filename2]" )

    Clean up the xml in filename1 and save the results to filename2.

  • $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>
      
  • $oject->add_node_after( [node], [prev node], [nodeset name], name => value, name2 => value2, ... )

    Similar to add_node above, but adds the node immediately after the passed [prev node].

  • $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]" )

    find_by_perl evaluates some perl code for each node in a set of nodes, and returns the nodes where the perl code evaluates as true. In order to easily reference node values, node values can be directly referred to from within the perl code by the name of the node with a dash(-) in front of the name. See the example below.

    Note that this function returns an array reference as opposed to a single node unlike the find_node function.

    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] )

    Merges the nodes from nodeset2 into nodeset1, matching the contents of each node based up the content in the id node.

    Example:

    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( ... )

    Works exactly like find_by_perl, but deletes whatever matches.

  • XML::Bare::forcearray( [noderef] )

    Turns the node reference into an array reference, whether that node is just a single node, or is already an array reference.

  • XML::Bare::new_node( ... )

    Creates a new node...

  • XML::Bare::newhash( ... )

    Creates a new hash with the specified value.

  • XML::Bare::simplify( [noderef] )

    Take a node with children that have immediate values and creates a hashref to reference those values by the name of each child.

Functions Used Internally

  • XML::Bare::c_parse()

  • XML::Bare::c_parsefile()

  • XML::Bare::free_tree()

  • XML::Bare::xml2obj()

  • XML::Bare::xml2obj_simple()

  • XML::Bare::obj2xml()

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

  • XML::TreePP

  • XML::Trivial

  • XML::SAX::Simple

  • XML::Grove::Builder

  • XML::XPath::XMLParser

To 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                2.7115   195.9411 26.5704
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.0165

Here 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::Smart                 6.8489   45.4236  16.2809
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.0145

These 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 hash 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 files created to do such testing are available upon request.

LICENSE

Copyright (C) 2007 David Helkowski

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.  You may also can
redistribute it and/or modify it under the terms of the Perl
Artistic License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.