View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Martin Bušík > XML-Traverse-ParseTree-0.03 > XML::Traverse::ParseTree



Annotate this POD

View/Report Bugs
Module Version: 0.03   Source  


XML::Traverse::ParseTree - iterators and getters for xml-access


    my $xml = XML::Parser->new(Style => "Tree")->parse($xmlcont);
    my $h   = XML::Traverse::ParseTree->new();

    my $a1  = $h->get($xml,'document','section','entries');
    my $i   = $h->child_iterator($a1);
    while (my $e = $i->()) {
        $attr = $h->get($e,'another-child-element','@attribute-name');
        $text = $h->get($e,'#TEXT');
    my $filter = sub { ... }
    my $i   = $h->child_iterator($xml,$filter);
    while (my $e = $i->()) {
    my $i = $h->get($xml,'section[*]','sections[*]','#TEXT');
    my $i = $h->get($xml,'//sections');
    my $i = $h->get($xml,'section[2],'sections[3]','*');


XML::Traverse::ParseTree supplies iterators and getters for accessing the contents of a xml content. The xml content must be already parsed using XML::Parser (tree-style)



Creates an instance of XML::Traverse::ParseTree. Currently, this instance does not have an intrinsic state. Although it could be used in a static way, this is not recommended. (Possible extention: support for different character encodings)


Returns the element name of the current element.


Return all attributes of the current element.


Returns the text of the current element.

get($parse_tree,access_path [,access_path ...])

General purpose access method. Depending on the access path elements, it returns an iterator ("iterator-context") or an scalar value. Returned value may be an element (position in the parse tree), an attribute value, all attributes of an element or the contents of text node.

Access path may consist of one or more entries. Each entry specifies a hierarchy level. The last one specifies if a attribute value is requested (prefix @) or the text (special value of #TEXT) or an element (position in the parse tree). Examples:

    $h->get($current,'@id') - returns the value of the attribute "id" of the current element
    $h->get($current,'a-child') - returns the first child element named "a-child"
    $h->get($current,'#TEXT') - returns the text node of the current element
    $h->get($current,'section[2]') - returns the second section child element
    $h->get($current,'section[*]') - returns an iterator over all child elements named section
    $h->get($current,'//section')  - returns an iterator over all child elements named section on all hierarchy levels AT and BELOW $current

More than one entry in the access path means more hierarchy levels, e.g.:


Returns the value of the attribute "id" of the element "section" which is a child element of an element "sections", which in turn is a child element of an element named "document", the "document" element is a child of the current element. (xpath-style: document/sections/section/@id)


Returns the text of the element document (and all of its children), which is a child element of current.


Then only "abcchilddef" will be returned.

More (advanced) examples:


Returns an iterator over all sub2 elements which are child elements of all sub1 elements, which in turn are child elements of $current.


Returns an iterator over sub2 Elements. Only those sub2 elements will be returned which are on second position relative to their respective parents. Example:

            <sub2 id="1"/>
            <sub2 id="2"/>
            <sub2 id="3"/>
            <sub2 id="4"/>
            <sub2 id="5"/>
            <sub2 id="6"/>

With the above mentioned get:


an iterator is returned, it delivers elements with the following ids: 2 and 5.


returns an iterator which delivers text content of all sub1 elements. Caution: Does a sub1 element has no text at all, undef is returned. This undef cannot be distinguished from undef used to terminate the iteration.


returns a hashref containing all attributes of the current element (no iterator!)


returns an iterator over all sub2 elements on all hierarchy levels below the third sub1 element.


returns an iterator over child elements (one hierarchy level below $current). When neither $name or $coderef is given, all child elements will be iterated.

If a name (scalar) is given, only child elements with that name will be iterated.

If a codereff is given, only those child elements will be iterated, for which the given function evaluates to true. The respective element is passed as parameter. Example:

    my $filter = sub {
        $pkg->get($_[0],'@class') eq "heading" ||
        defined $pkg->get($_[0],'@style')
    $i = $pkg->child_iterator($current,$filter);

returns an iterator over the current element and child elements on all hierarchy levels. The order is depth-first (exactly: current, then childs). Regarding the meaning of $name and $filter see child_iterator above.


Creates a hashref with the contens of the current element (experimental)

getter(access_path [,access_path ...])

Returns a curried version of get(), this is usefull in cases where the same access path is used in different places. Example:

    *get_id = $pkg->getter('@id');

    $i = $pkg->child_iterator($xml);
    while(my $e = $i->()) {
        if (get_id($e) eq '45')) {


None known.


  Concerning the concepts of iterators using closures/anonymous subs: 


  Martin Busik <>


Copyright (c) 2006 by Martin Busik. All rights reserved.

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


syntax highlighting: