perldoc > XML::TreePP

📛 NAME

XML::TreePP — Pure Perl implementation for parsing/writing XML documents

🚀 Quick Reference

Use CaseCommandDescription
Parse XML file$tpp->parsefile("file.xml")Read an XML file and return a hash tree
Parse XML string$tpp->parse($xml_string)Convert XML string to a hash tree
Fetch remote XML via HTTP GET$tpp->parsehttp(GET => $url)Get XML from a URL and parse it
Fetch remote XML via HTTP POST$tpp->parsehttp(POST => $url, $body)POST data and parse returned XML
Write hash tree to XML$tpp->write($tree)Generate XML string from a hash tree
Write hash tree to file$tpp->writefile("file.xml", $tree)Write XML document to a file
Force array for elements$tpp->set(force_array => ['item'])Ensure certain elements are always arrays

🔧 SYNOPSIS

Parse an XML document from file into hash tree:

use XML::TreePP;
my $tpp = XML::TreePP->new();
my $tree = $tpp->parsefile( "index.rdf" );
print "Title: ", $tree->{"rdf:RDF"}->{item}->[0]->{title}, "\n";
print "URL:   ", $tree->{"rdf:RDF"}->{item}->[0]->{link}, "\n";

Write an XML document as string from hash tree:

use XML::TreePP;
my $tpp = XML::TreePP->new();
my $tree = { rss => { channel => { item => [ {
    title   => "The Perl Directory",
    link    => "http://www.perl.org/",
}, {
    title   => "The Comprehensive Perl Archive Network",
    link    => "http://cpan.perl.org/",
} ] } } };
my $xml = $tpp->write( $tree );
print $xml;

Get a remote XML document by HTTP-GET and parse it into hash tree:

use XML::TreePP;
my $tpp = XML::TreePP->new();
my $tree = $tpp->parsehttp( GET => "http://use.perl.org/index.rss" );
print "Title: ", $tree->{"rdf:RDF"}->{channel}->{title}, "\n";
print "URL:   ", $tree->{"rdf:RDF"}->{channel}->{link}, "\n";

Get a remote XML document by HTTP-POST and parse it into hash tree:

use XML::TreePP;
my $tpp = XML::TreePP->new( force_array => [qw( item )] );
my $cgiurl = "http://search.hatena.ne.jp/keyword";
my $keyword = "ajax";
my $cgiquery = "mode=rss2&word=".$keyword;
my $tree = $tpp->parsehttp( POST => $cgiurl, $cgiquery );
print "Link: ", $tree->{rss}->{channel}->{item}->[0]->{link}, "\n";
print "Desc: ", $tree->{rss}->{channel}->{item}->[0]->{description}, "\n";

📖 DESCRIPTION

XML::TreePP module parses an XML document and expands it for a hash tree. This generates an XML document from a hash tree as the opposite way around. This is a pure Perl implementation and requires no modules depended. This can also fetch and parse an XML document from remote web server like the XMLHttpRequest object does at JavaScript language.

💡 EXAMPLES

📄 Parse XML file

Sample XML document:

<?xml version="1.0" encoding="UTF-8"?>
<family name="Kawasaki">
    <father>Yasuhisa</father>
    <mother>Chizuko</mother>
    <children>
        <girl>Shiori</girl>
        <boy>Yusuke</boy>
        <boy>Kairi</boy>
    </children>
</family>

Sample program to read a xml file and dump it:

use XML::TreePP;
use Data::Dumper;
my $tpp = XML::TreePP->new();
my $tree = $tpp->parsefile( "family.xml" );
my $text = Dumper( $tree );
print $text;

Result dumped:

$VAR1 = {
    'family' => {
        '-name' => 'Kawasaki',
        'father' => 'Yasuhisa',
        'mother' => 'Chizuko',
        'children' => {
            'girl' => 'Shiori'
            'boy' => [
                'Yusuke',
                'Kairi'
            ],
        }
    }
};

Details:

print $tree->{family}->{father};        # the father's given name.

The prefix '-' is added on every attribute's name.

print $tree->{family}->{"-name"};       # the family name of the family

The array is used because the family has two boys.

print $tree->{family}->{children}->{boy}->[1];  # The second boy's name
print $tree->{family}->{children}->{girl};      # The girl's name

📝 Text node and attributes

If a element has both of a text node and attributes or both of a text node and other child nodes, value of a text node is moved to #text like child nodes.

use XML::TreePP;
use Data::Dumper;
my $tpp = XML::TreePP->new();
my $source = '<span class="author">Kawasaki Yusuke</span>';
my $tree = $tpp->parse( $source );
my $text = Dumper( $tree );
print $text;

The result dumped is following:

$VAR1 = {
    'span' => {
        '-class' => 'author',
        '#text'  => 'Kawasaki Yusuke'
    }
};

The special node name of #text is used because this elements has attribute(s) in addition to the text node. See also text_node_key option.

đŸ› ī¸ METHODS

new

This constructor method returns a new XML::TreePP object with %options.

$tpp = XML::TreePP->new( %options );

set

This method sets a option value for option_name. If $option_value is not defined, its option is deleted.

$tpp->set( option_name => $option_value );

See OPTIONS section below for details.

get

This method returns a current option value for option_name.

$tpp->get( 'option_name' );

parse

This method reads an XML document by string and returns a hash tree converted. The first argument is a scalar or a reference to a scalar.

$tree = $tpp->parse( $source );

parsefile

This method reads an XML document by file and returns a hash tree converted. The first argument is a filename.

$tree = $tpp->parsefile( $file );

parsehttp

This method receives an XML document from a remote server via HTTP and returns a hash tree converted.

$tree = $tpp->parsehttp( $method, $url, $body, $head );

$method is a method of HTTP connection: GET/POST/PUT/DELETE $url is an URI of an XML file. $body is a request body when you use POST method. $head is a request headers as a hash ref. LWP::UserAgent module or HTTP::Lite module is required to fetch a file.

( $tree, $xml, $code ) = $tpp->parsehttp( $method, $url, $body, $head );

In array context, This method returns also raw XML document received and HTTP response's status code.

write

This method parses a hash tree and returns an XML document as a string.

$source = $tpp->write( $tree, $encode );

$tree is a reference to a hash tree.

writefile

This method parses a hash tree and writes an XML document into a file.

$tpp->writefile( $file, $tree, $encode );

$file is a filename to create. $tree is a reference to a hash tree.

âš™ī¸ OPTIONS FOR PARSING XML

This module accepts option parameters following:

âš™ī¸ OPTIONS FOR WRITING XML

âš™ī¸ OPTIONS FOR BOTH

👤 AUTHOR

Yusuke Kawasaki, http://www.kawa.net/

đŸ“Ļ REPOSITORY

https://github.com/kawanet/XML-TreePP

ÂŠī¸ COPYRIGHT

The following copyright notice applies to all the files provided in this distribution, including binary files, unless explicitly noted otherwise.

Copyright 2006-2010 Yusuke Kawasaki

📄 LICENSE

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

XML::TreePP
📛 NAME 🚀 Quick Reference 🔧 SYNOPSIS 📖 DESCRIPTION 💡 EXAMPLES
📄 Parse XML file 📝 Text node and attributes
đŸ› ī¸ METHODS
new set get parse parsefile parsehttp write writefile
âš™ī¸ OPTIONS FOR PARSING XML âš™ī¸ OPTIONS FOR WRITING XML âš™ī¸ OPTIONS FOR BOTH 👤 AUTHOR đŸ“Ļ REPOSITORY ÂŠī¸ COPYRIGHT 📄 LICENSE

Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-05 01:16 @216.73.217.1
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^