The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Lab::Data::XMLtree - Handle and store XML and perl data structures with precise declaration.

SYNOPSIS

    use Lab::Data::XMLtree;
    
    my $data_declaration = {
        info                     => [#              type B
            'SCALAR',
            {
                basename    => ['PSCALAR'],#        type A
                title       => ['SCALAR'],#         type A
                place       => ['SCALAR']#          type A
            }
        ],
        column                  => [#               type K
            'ARRAY',
            'id',
            {
                # PSCALAR means that this element will not
                # be saved. Does not work for YAML yet.
                min         => ['PSCALAR'],#        type A
                max         => ['PSCALAR'],#        type A
                description => ['SCALAR']#          type A
            }
        ],
        axis                    => [#               type F
            'HASH',
            'label',
            {
                unit        => ['SCALAR'],#         type A
                logscale    => ['SCALAR'],#         type A
                description => ['SCALAR']#          type A
            }
        ]
    };
    #create Lab::Data::XMLtree object from file
    $data=Lab::Data::XMLtree->read_xml($data_declaration,'filename.xml');

    #the autoloader
    # get
    print $data->info_title;
    # get with $id
    print $data->column_description($id);
    # set with $key and $value
    $data->axis_description($label,'descriptiontext');
   
    #save data as YAML
    $data->save_yaml('filename.yaml');

DESCRIPTION

Lab::Data::XMLtree will take you to similar spots as XML::Simple does, but in a bigger bus and with fewer wild animals.

That's not a bad thing. You get more control of the data transformation processes and you get some extra functionality.

DATA DECLARATION

Lab::Data::XMLtree uses a data declaration, that describes, what the perl data structure looks like, and how this data structure is converted to XML.

CONSTRUCTORS

new($declaration,[$data])

Create a new Lab::Data::XMLtree. $data must be hashref and should match the declaration. Returns Lab::XMLtree object.

The first two elements define the folding behaviour.

SCALAR|PSCALAR

Element occurs zero or one time. No folding necessary.

Examples:

    $data->{dataset_title}='content';
ARRAY|PARRAY

Element occurs zero or more times. Folding will be done using an array reference. If $id is given, this XML element will be used as an id.

Example:

    $data->{column}->[4]->{label}='testlabel';
HASH|PHASH

Element occurs zero or more times. Folding will be done using a hash reference. If $key is given, this XML element will be used as a key.

Example:

    $data->{axis}->{gate voltage}->{unit}="mV";

read_xml($declaration,$filename)

Opens a XML file $filename. Returns Lab::Data::XMLtree object.

read_yaml($declaration,$filename)

Opens a YAML file $filename. Returns Lab::Data::XMLtree object.

METHODS

merge_tree($tree)

Merge another Lab::Data::XMLtree into this one. Other tree must not necessarily be blessed.

save_xml($filename)

Saves the tree as XML to $filename.

save_yaml($filename)

Saves the tree as YAML to $filename. PSCALAR etc. don't work yet.

to_string()

Returns a stringified version of the object. (Using Data::Dumper.)

autoload

Get/set anything you want. Accounts the data declaration.

PRIVATE FUNCTIONS

_load_xml($declaration,$filename)
_merge_node_lists($declaration,$destination_perlnode_list,$source_perlnode_list)
_parse_domnode_list($domnode_list,$defnode_list)
_write_node_list($generator,$defnode_list,$perlnode_list)
_getset_node_list_from_string($perlnode_list,$defnode_list,$nodes_string)
_get_defnode_type($defnode)
_magic_keys($defnode_list,$perlnode_list,$node_name,[@types])
_magic_get_perlnode($defnode_list,$perlnode_list,$node_name,$key,[@types])
_magic_set_perlnode($defnode_list,$perlnode_list,$node_name,$key,$value,[@types])

CAVEATS/BUGS

Lab::Data::XMLtree does not support all possible kinds of perl data structures. It is also not too flexible when it comes to XML. It simply supports something that I needed.

SEE ALSO

XML::Simple

Lab::Data::XMLtree is similar to XML::Simple (XML::Simple).

XML::DOM

Lab::Data::XMLtree can use XML::DOM (XML::DOM) to retrieve stored data.

XML::Generator

Lab::XMLtree can use XML::Generator (XML::Generator) to store data as XML.

YAML

Lab::XMLtree can use YAML (YAML) for data storage.

AUTHOR/COPYRIGHT

Copyright 2004-2006 Daniel Schröer (http://www.danielschroeer.de), 2011 Andreas K. Hüttel

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.