{
    "content": [
        {
            "type": "text",
            "text": "# XML::TreePP (perldoc)\n\n## NAME\n\nXML::TreePP -- Pure Perl implementation for parsing/writing XML documents\n\n## SYNOPSIS\n\nparse an XML document from file into hash tree:\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new();\nmy $tree = $tpp->parsefile( \"index.rdf\" );\nprint \"Title: \", $tree->{\"rdf:RDF\"}->{item}->[0]->{title}, \"\\n\";\nprint \"URL:   \", $tree->{\"rdf:RDF\"}->{item}->[0]->{link}, \"\\n\";\nwrite an XML document as string from hash tree:\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new();\nmy $tree = { rss => { channel => { item => [ {\ntitle   => \"The Perl Directory\",\nlink    => \"http://www.perl.org/\",\n}, {\ntitle   => \"The Comprehensive Perl Archive Network\",\nlink    => \"http://cpan.perl.org/\",\n} ] } } };\nmy $xml = $tpp->write( $tree );\nprint $xml;\nget a remote XML document by HTTP-GET and parse it into hash tree:\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new();\nmy $tree = $tpp->parsehttp( GET => \"http://use.perl.org/index.rss\" );\nprint \"Title: \", $tree->{\"rdf:RDF\"}->{channel}->{title}, \"\\n\";\nprint \"URL:   \", $tree->{\"rdf:RDF\"}->{channel}->{link}, \"\\n\";\nget a remote XML document by HTTP-POST and parse it into hash tree:\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new( forcearray => [qw( item )] );\nmy $cgiurl = \"http://search.hatena.ne.jp/keyword\";\nmy $keyword = \"ajax\";\nmy $cgiquery = \"mode=rss2&word=\".$keyword;\nmy $tree = $tpp->parsehttp( POST => $cgiurl, $cgiquery );\nprint \"Link: \", $tree->{rss}->{channel}->{item}->[0]->{link}, \"\\n\";\nprint \"Desc: \", $tree->{rss}->{channel}->{item}->[0]->{description}, \"\\n\";\n\n## DESCRIPTION\n\nXML::TreePP module parses an XML document and expands it for a hash tree. This generates an XML\ndocument from a hash tree as the opposite way around. This is a pure Perl implementation and\nrequires no modules depended. This can also fetch and parse an XML document from remote web\nserver like the XMLHttpRequest object does at JavaScript language.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **EXAMPLES** (2 subsections)\n- **METHODS**\n- **OPTIONS FOR PARSING XML**\n- **OPTIONS FOR WRITING XML**\n- **OPTIONS FOR BOTH**\n- **AUTHOR**\n- **REPOSITORY**\n- **COPYRIGHT**\n- **LICENSE**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "XML::TreePP",
        "section": "",
        "mode": "perldoc",
        "summary": "XML::TreePP -- Pure Perl implementation for parsing/writing XML documents",
        "synopsis": "parse an XML document from file into hash tree:\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new();\nmy $tree = $tpp->parsefile( \"index.rdf\" );\nprint \"Title: \", $tree->{\"rdf:RDF\"}->{item}->[0]->{title}, \"\\n\";\nprint \"URL:   \", $tree->{\"rdf:RDF\"}->{item}->[0]->{link}, \"\\n\";\nwrite an XML document as string from hash tree:\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new();\nmy $tree = { rss => { channel => { item => [ {\ntitle   => \"The Perl Directory\",\nlink    => \"http://www.perl.org/\",\n}, {\ntitle   => \"The Comprehensive Perl Archive Network\",\nlink    => \"http://cpan.perl.org/\",\n} ] } } };\nmy $xml = $tpp->write( $tree );\nprint $xml;\nget a remote XML document by HTTP-GET and parse it into hash tree:\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new();\nmy $tree = $tpp->parsehttp( GET => \"http://use.perl.org/index.rss\" );\nprint \"Title: \", $tree->{\"rdf:RDF\"}->{channel}->{title}, \"\\n\";\nprint \"URL:   \", $tree->{\"rdf:RDF\"}->{channel}->{link}, \"\\n\";\nget a remote XML document by HTTP-POST and parse it into hash tree:\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new( forcearray => [qw( item )] );\nmy $cgiurl = \"http://search.hatena.ne.jp/keyword\";\nmy $keyword = \"ajax\";\nmy $cgiquery = \"mode=rss2&word=\".$keyword;\nmy $tree = $tpp->parsehttp( POST => $cgiurl, $cgiquery );\nprint \"Link: \", $tree->{rss}->{channel}->{item}->[0]->{link}, \"\\n\";\nprint \"Desc: \", $tree->{rss}->{channel}->{item}->[0]->{description}, \"\\n\";",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "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",
            "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 \"textnodekey\" option."
        ],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 41,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 1,
                "subsections": [
                    {
                        "name": "Parse XML file",
                        "lines": 52
                    },
                    {
                        "name": "Text node and attributes:",
                        "lines": 23
                    }
                ]
            },
            {
                "name": "METHODS",
                "lines": 59,
                "subsections": []
            },
            {
                "name": "OPTIONS FOR PARSING XML",
                "lines": 93,
                "subsections": []
            },
            {
                "name": "OPTIONS FOR WRITING XML",
                "lines": 42,
                "subsections": []
            },
            {
                "name": "OPTIONS FOR BOTH",
                "lines": 37,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "REPOSITORY",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COPYRIGHT",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "LICENSE",
                "lines": 3,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "XML::TreePP -- Pure Perl implementation for parsing/writing XML documents\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "parse an XML document from file into hash tree:\n\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new();\nmy $tree = $tpp->parsefile( \"index.rdf\" );\nprint \"Title: \", $tree->{\"rdf:RDF\"}->{item}->[0]->{title}, \"\\n\";\nprint \"URL:   \", $tree->{\"rdf:RDF\"}->{item}->[0]->{link}, \"\\n\";\n\nwrite an XML document as string from hash tree:\n\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new();\nmy $tree = { rss => { channel => { item => [ {\ntitle   => \"The Perl Directory\",\nlink    => \"http://www.perl.org/\",\n}, {\ntitle   => \"The Comprehensive Perl Archive Network\",\nlink    => \"http://cpan.perl.org/\",\n} ] } } };\nmy $xml = $tpp->write( $tree );\nprint $xml;\n\nget a remote XML document by HTTP-GET and parse it into hash tree:\n\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new();\nmy $tree = $tpp->parsehttp( GET => \"http://use.perl.org/index.rss\" );\nprint \"Title: \", $tree->{\"rdf:RDF\"}->{channel}->{title}, \"\\n\";\nprint \"URL:   \", $tree->{\"rdf:RDF\"}->{channel}->{link}, \"\\n\";\n\nget a remote XML document by HTTP-POST and parse it into hash tree:\n\nuse XML::TreePP;\nmy $tpp = XML::TreePP->new( forcearray => [qw( item )] );\nmy $cgiurl = \"http://search.hatena.ne.jp/keyword\";\nmy $keyword = \"ajax\";\nmy $cgiquery = \"mode=rss2&word=\".$keyword;\nmy $tree = $tpp->parsehttp( POST => $cgiurl, $cgiquery );\nprint \"Link: \", $tree->{rss}->{channel}->{item}->[0]->{link}, \"\\n\";\nprint \"Desc: \", $tree->{rss}->{channel}->{item}->[0]->{description}, \"\\n\";\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "XML::TreePP module parses an XML document and expands it for a hash tree. This generates an XML\ndocument from a hash tree as the opposite way around. This is a pure Perl implementation and\nrequires no modules depended. This can also fetch and parse an XML document from remote web\nserver like the XMLHttpRequest object does at JavaScript language.\n",
                "subsections": []
            },
            "EXAMPLES": {
                "content": "",
                "subsections": [
                    {
                        "name": "Parse XML file",
                        "content": "Sample XML document:\n\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<family name=\"Kawasaki\">\n<father>Yasuhisa</father>\n<mother>Chizuko</mother>\n<children>\n<girl>Shiori</girl>\n<boy>Yusuke</boy>\n<boy>Kairi</boy>\n</children>\n</family>\n\nSample program to read a xml file and dump it:\n\nuse XML::TreePP;\nuse Data::Dumper;\nmy $tpp = XML::TreePP->new();\nmy $tree = $tpp->parsefile( \"family.xml\" );\nmy $text = Dumper( $tree );\nprint $text;\n\nResult dumped:\n\n$VAR1 = {\n'family' => {\n'-name' => 'Kawasaki',\n'father' => 'Yasuhisa',\n'mother' => 'Chizuko',\n'children' => {\n'girl' => 'Shiori'\n'boy' => [\n'Yusuke',\n'Kairi'\n],\n}\n}\n};\n\nDetails:\n\nprint $tree->{family}->{father};        # the father's given name.\n\nThe prefix '-' is added on every attribute's name.\n\nprint $tree->{family}->{\"-name\"};       # the family name of the family\n\nThe array is used because the family has two boys.\n\nprint $tree->{family}->{children}->{boy}->[1];  # The second boy's name\nprint $tree->{family}->{children}->{girl};      # The girl's name\n"
                    },
                    {
                        "name": "Text node and attributes:",
                        "content": "If a element has both of a text node and attributes or both of a text node and other child\nnodes, value of a text node is moved to \"#text\" like child nodes.\n\nuse XML::TreePP;\nuse Data::Dumper;\nmy $tpp = XML::TreePP->new();\nmy $source = '<span class=\"author\">Kawasaki Yusuke</span>';\nmy $tree = $tpp->parse( $source );\nmy $text = Dumper( $tree );\nprint $text;\n\nThe result dumped is following:\n\n$VAR1 = {\n'span' => {\n'-class' => 'author',\n'#text'  => 'Kawasaki Yusuke'\n}\n};\n\nThe special node name of \"#text\" is used because this elements has attribute(s) in addition to\nthe text node. See also \"textnodekey\" option.\n"
                    }
                ]
            },
            "METHODS": {
                "content": "new\nThis constructor method returns a new XML::TreePP object with %options.\n\n$tpp = XML::TreePP->new( %options );\n\nset\nThis method sets a option value for \"optionname\". If $optionvalue is not defined, its option\nis deleted.\n\n$tpp->set( optionname => $optionvalue );\n\nSee OPTIONS section below for details.\n\nget\nThis method returns a current option value for \"optionname\".\n\n$tpp->get( 'optionname' );\n\nparse\nThis method reads an XML document by string and returns a hash tree converted. The first\nargument is a scalar or a reference to a scalar.\n\n$tree = $tpp->parse( $source );\n\nparsefile\nThis method reads an XML document by file and returns a hash tree converted. The first argument\nis a filename.\n\n$tree = $tpp->parsefile( $file );\n\nparsehttp\nThis method receives an XML document from a remote server via HTTP and returns a hash tree\nconverted.\n\n$tree = $tpp->parsehttp( $method, $url, $body, $head );\n\n$method is a method of HTTP connection: GET/POST/PUT/DELETE $url is an URI of an XML file. $body\nis a request body when you use POST method. $head is a request headers as a hash ref.\nLWP::UserAgent module or HTTP::Lite module is required to fetch a file.\n\n( $tree, $xml, $code ) = $tpp->parsehttp( $method, $url, $body, $head );\n\nIn array context, This method returns also raw XML document received and HTTP response's status\ncode.\n\nwrite\nThis method parses a hash tree and returns an XML document as a string.\n\n$source = $tpp->write( $tree, $encode );\n\n$tree is a reference to a hash tree.\n\nwritefile\nThis method parses a hash tree and writes an XML document into a file.\n\n$tpp->writefile( $file, $tree, $encode );\n\n$file is a filename to create. $tree is a reference to a hash tree.\n",
                "subsections": []
            },
            "OPTIONS FOR PARSING XML": {
                "content": "This module accepts option parameters following:\n\nforcearray\nThis option allows you to specify a list of element names which should always be forced into an\narray representation.\n\n$tpp->set( forcearray => [ 'rdf:li', 'item', '-xmlns' ] );\n\nThe default value is null, it means that context of the elements will determine to make array or\nto keep it scalar or hash. Note that the special wildcard name '*' means all elements.\n\nforcehash\nThis option allows you to specify a list of element names which should always be forced into an\nhash representation.\n\n$tpp->set( forcehash => [ 'item', 'image' ] );\n\nThe default value is null, it means that context of the elements will determine to make hash or\nto keep it scalar as a text node. See also \"textnodekey\" option below. Note that the special\nwildcard name '*' means all elements.\n\ncdatascalarref\nThis option allows you to convert a cdata section into a reference for scalar on parsing an XML\ndocument.\n\n$tpp->set( cdatascalarref => 1 );\n\nThe default value is false, it means that each cdata section is converted into a scalar.\n\nuseragent\nThis option allows you to specify a HTTPUSERAGENT string which is used by parsehttp() method.\n\n$tpp->set( useragent => 'Mozilla/4.0 (compatible; ...)' );\n\nThe default string is 'XML-TreePP/#.##', where '#.##' is substituted with the version number of\nthis library.\n\nhttplite\nThis option forces pasrsehttp() method to use a HTTP::Lite instance.\n\nmy $http = HTTP::Lite->new();\n$tpp->set( httplite => $http );\n\nlwpuseragent\nThis option forces parsehttp() method to use a LWP::UserAgent instance.\n\nmy $ua = LWP::UserAgent->new();\n$ua->timeout( 60 );\n$ua->envproxy;\n$tpp->set( lwpuseragent => $ua );\n\nYou may use this with LWP::UserAgent::WithCache.\n\nbaseclass\nThis blesses class name for each element's hashref. Each class is named straight as a child\nclass of it parent class.\n\n$tpp->set( baseclass => 'MyElement' );\nmy $xml  = '<root><parent><child key=\"val\">text</child></parent></root>';\nmy $tree = $tpp->parse( $xml );\nprint ref $tree->{root}->{parent}->{child}, \"\\n\";\n\nA hash for <child> element above is blessed to \"MyElement::root::parent::child\" class. You may\nuse this with Class::Accessor.\n\nelemclass\nThis blesses class name for each element's hashref. Each class is named horizontally under the\ndirect child of \"MyElement\".\n\n$tpp->set( baseclass => 'MyElement' );\nmy $xml  = '<root><parent><child key=\"val\">text</child></parent></root>';\nmy $tree = $tpp->parse( $xml );\nprint ref $tree->{root}->{parent}->{child}, \"\\n\";\n\nA hash for <child> element above is blessed to \"MyElement::child\" class.\n\nxmlderef\nThis option dereferences the numeric character references, like &#xEB;, &#28450;, etc., in an\nXML document when this value is true.\n\n$tpp->set( xmlderef => 1 );\n\nNote that, for security reasons and your convenient, this module dereferences the predefined\ncharacter entity references, &amp;, &lt;, &gt;, &apos; and &quot;, and the numeric character\nreferences up to U+007F without xmlderef per default.\n\nrequirexmldecl\nThis option requires XML declaration at the top of XML document to parse.\n\n$tpp->set( requirexmldecl => 1 );\n\nThis will die when <?xml .../?> declration not found.\n",
                "subsections": []
            },
            "OPTIONS FOR WRITING XML": {
                "content": "firstout\nThis option allows you to specify a list of element/attribute names which should always appears\nat first on output XML document.\n\n$tpp->set( firstout => [ 'link', 'title', '-type' ] );\n\nThe default value is null, it means alphabetical order is used.\n\nlastout\nThis option allows you to specify a list of element/attribute names which should always appears\nat last on output XML document.\n\n$tpp->set( lastout => [ 'items', 'item', 'entry' ] );\n\nindent\nThis makes the output more human readable by indenting appropriately.\n\n$tpp->set( indent => 2 );\n\nThis doesn't strictly follow the XML specification but does looks nice.\n\nxmldecl\nThis module inserts an XML declaration on top of the XML document generated per default. This\noption forces to change it to another or just remove it.\n\n$tpp->set( xmldecl => '' );\n\noutputencoding\nThis option allows you to specify a encoding of the XML document generated by write/writefile\nmethods.\n\n$tpp->set( outputencoding => 'UTF-8' );\n\nOn Perl 5.8.0 and later, you can select it from every encodings supported by Encode.pm. On Perl\n5.6.x and before with Jcode.pm, you can use \"ShiftJIS\", \"EUC-JP\", \"ISO-2022-JP\" and \"UTF-8\".\nThe default value is \"UTF-8\" which is recommended encoding.\n\nemptyelementtagend\n$tpp->set( emptyelementtagend => '>' );\n\nSet characters which close empty tag. The default value is ' />'.\n",
                "subsections": []
            },
            "OPTIONS FOR BOTH": {
                "content": "utf8flag\nThis makes utf8 flag on for every element's value parsed and makes it on for the XML document\ngenerated as well.\n\n$tpp->set( utf8flag => 1 );\n\nPerl 5.8.1 or later is required to use this.\n\nattrprefix\nThis option allows you to specify a prefix character(s) which is inserted before each attribute\nnames.\n\n$tpp->set( attrprefix => '@' );\n\nThe default character is '-'. Or set '@' to access attribute values like E4X, ECMAScript for\nXML. Zero-length prefix '' is available as well, it means no prefix is added.\n\ntextnodekey\nThis option allows you to specify a hash key for text nodes.\n\n$tpp->set( textnodekey => '#text' );\n\nThe default key is \"#text\".\n\nignoreerror\nThis module calls Carp::croak function on an error per default. This option makes all errors\nignored and just returns.\n\n$tpp->set( ignoreerror => 1 );\n\nuseixhash\nThis option keeps the order for each element appeared in XML. Tie::IxHash module is required.\n\n$tpp->set( useixhash => 1 );\n\nThis makes parsing performance slow. (about 100% slower than default)\n",
                "subsections": []
            },
            "AUTHOR": {
                "content": "Yusuke Kawasaki, http://www.kawa.net/\n",
                "subsections": []
            },
            "REPOSITORY": {
                "content": "https://github.com/kawanet/XML-TreePP\n",
                "subsections": []
            },
            "COPYRIGHT": {
                "content": "The following copyright notice applies to all the files provided in this distribution, including\nbinary files, unless explicitly noted otherwise.\n\nCopyright 2006-2010 Yusuke Kawasaki\n",
                "subsections": []
            },
            "LICENSE": {
                "content": "This library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
                "subsections": []
            }
        }
    }
}