# perldoc > XML::TreePP

---
type: CommandReference
command: XML::TreePP
mode: perldoc
section: ""
source: perldoc
---

## Quick Reference

- `XML::TreePP->new(%options)` — create a new parser/writer object
- `$tpp->parse($source)` — parse XML string into hash tree
- `$tpp->parsefile($file)` — parse XML file into hash tree
- `$tpp->parsehttp($method, $url, $body, $head)` — fetch and parse XML via HTTP
- `$tpp->write($tree, $encode)` — generate XML string from hash tree
- `$tpp->writefile($file, $tree, $encode)` — write XML to file
- `$tpp->set($option, $value)` — set option value
- `$tpp->get($option)` — get current option value

## Name

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

## Synopsis

perl
use XML::TreePP;

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

# Write XML from hash
my $tree = { rss => { channel => { item => [
    { title => "The Perl Directory",   link => "http://www.perl.org/" },
    { title => "CPAN",                 link => "http://cpan.perl.org/" },
] } } };
my $xml = $tpp->write($tree);
print $xml;

# Fetch and parse remote XML via HTTP GET
my $tree = $tpp->parsehttp(GET => "http://use.perl.org/index.rss");

# Fetch via HTTP POST
my $tree = $tpp->parsehttp(POST => $cgiurl, $cgiquery);
## Options

### Parsing Options

- `force_array => \@list` — always represent listed elements as arrays (use `'*'` for all)
- `force_hash => \@list` — always represent listed elements as hashes (use `'*'` for all)
- `cdata_scalar_ref => 1` — convert CDATA sections to scalar references
- `user_agent => $string` — HTTP User-Agent string for `parsehttp()`
- `http_lite => $http_lite_obj` — force use of HTTP::Lite
- `lwp_useragent => $lwp_ua_obj` — force use of LWP::UserAgent
- `base_class => $class` — bless each element hashref into a nested class (e.g., `MyElement::root::parent::child`)
- `elem_class => $class` — bless each element hashref into a flat class under `$class` (e.g., `MyElement::child`)
- `xml_deref => 1` — dereference numeric character references (&#xEB; etc.)
- `require_xml_decl => 1` — die if XML declaration missing

### Writing Options

- `first_out => \@list` — element/attribute names to output first (default: alphabetical)
- `last_out => \@list` — element/attribute names to output last
- `indent => $n` — indent output with `$n` spaces (for readability)
- `xml_decl => ''` — remove XML declaration; set to other string to replace
- `output_encoding => $encoding` — encoding of generated XML (default: UTF-8)
- `empty_element_tag_end => '>'` — closing for empty tags (default: `' />'`)

### General Options

- `utf8_flag => 1` — enable UTF-8 flag on parsed values and generated output (requires Perl 5.8.1+)
- `attr_prefix => $prefix` — prefix for attribute names in hash (default: `-`; use `'@'` for E4X style)
- `text_node_key => $key` — hash key for text nodes when element has attributes (default: `#text`)
- `ignore_error => 1` — suppress errors (default: croak on error)
- `use_ixhash => 1` — preserve element order using Tie::IxHash (slower)

## Examples

### Parse XML file

xml
<?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>
perl
use XML::TreePP;
use Data::Dumper;
my $tpp = XML::TreePP->new();
my $tree = $tpp->parsefile("family.xml");
print Dumper($tree);
Result:

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

perl
$tree->{family}->{father};                    # "Yasuhisa"
$tree->{family}->{"-name"};                   # "Kawasaki" (attribute)
$tree->{family}->{children}->{boy}->[1];      # "Kairi"
$tree->{family}->{children}->{girl};          # "Shiori"
### Text node and attributes

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

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

- [XML::TreePP](https://metacpan.org/pod/XML::TreePP) on CPAN
- [XML::Simple](https://metacpan.org/pod/XML::Simple) — alternative XML parser
- [XML::Twig](https://metacpan.org/pod/XML::Twig) — another XML processing tool
- [LWP::UserAgent](https://metacpan.org/pod/LWP::UserAgent) — HTTP client for `parsehttp()`
- [HTTP::Lite](https://metacpan.org/pod/HTTP::Lite) — lightweight HTTP client