NAME

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

VERSION

0.01

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

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 but to XML, the nodes are then sorted and output in the correct order to XML.

  • Comments are parsed, but discarded

    Comments can exist in the XML, but they are thrown away during parsing.

  • PI sections are parsed, but discarded

  • Unknown <! sections are parsed, but discarded

  • Attributes must use double quotes if quoted

    Attributes in XML can be used, with or without quotes, but if quotes are used they must be double quotes. If single quotes are used, the value will end up starting with a single quote and continue until a space or a node end.

  • Quoted attributes cannot contain escaped quotes

    No escape character is recognized within quotes. As a result, there is no way to store a double quote character in an attribute value.

  • 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

$xml->parse();

$xml->add_node( $root->{xml}, 'item', name => 'Bob' );
<xml><item><name>Bob</name></item></xml>

<xml> <a><b>1</b></a> <a><b>2</b></a> </xml>
$xml->del_node( $root->{xml}, 'a', b=>'1' );
<xml> <a><b>2</b></a> </xml>

<xml> <ob> <key>1</key> <val>a</val> </ob> <ob> <key>2</key> <val>b</val> </ob> </xml>
$xml->find_node( $root->{xml}, 'ob', key => '1' )->{val}->{value} = 'test';
<xml> <ob> <key>1</key> <val>test</val> </ob> <ob> <key>2</key> <val>b</val> </ob> </xml>

print $xml->xml( $root );
( prints clean xml output )

$xml->save();
( saves to file location specified when xml object created ):q

LICENSE

XML::Bare version 0.01 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.