{
    "mode": "perldoc",
    "parameter": "XML::TokeParser",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/XML%3A%3ATokeParser/json",
    "generated": "2026-06-12T14:37:51Z",
    "synopsis": "use XML::TokeParser;\n#\n#parse from file\nmy $p = XML::TokeParser->new('file.xml')\n#\n#parse from open handle\nopen IN, 'file.xml' or die $!;\nmy $p = XML::TokeParser->new( \\*IN, Noempty => 1 );\n#\n#parse literal text\nmy $text = '<tag xmlns=\"http://www.omsdev.com\">text</tag>';\nmy $p    = XML::TokeParser->new( \\$text, Namespaces => 1 );\n#\n#read next token\nmy $token = $p->gettoken();\n#\n#skip to <title> and read text\n$p->gettag('title');\n$p->gettext();\n#\n#read text of next <para>, ignoring any internal markup\n$p->gettag('para');\n$p->gettrimmedtext('/para');\n#\n#process <para> if interesting text\n$t = $p->gettag('para');\n$p->beginsaving($t);\nif ( $p->gettrimmedtext('/para') =~ /interesting stuff/ ) {\n$p->restoresaved();\nprocesspara($p);\n}",
    "sections": {
        "NAME": {
            "content": "XML::TokeParser - Simplified interface to XML::Parser\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use XML::TokeParser;\n#\n#parse from file\nmy $p = XML::TokeParser->new('file.xml')\n#\n#parse from open handle\nopen IN, 'file.xml' or die $!;\nmy $p = XML::TokeParser->new( \\*IN, Noempty => 1 );\n#\n#parse literal text\nmy $text = '<tag xmlns=\"http://www.omsdev.com\">text</tag>';\nmy $p    = XML::TokeParser->new( \\$text, Namespaces => 1 );\n#\n#read next token\nmy $token = $p->gettoken();\n#\n#skip to <title> and read text\n$p->gettag('title');\n$p->gettext();\n#\n#read text of next <para>, ignoring any internal markup\n$p->gettag('para');\n$p->gettrimmedtext('/para');\n#\n#process <para> if interesting text\n$t = $p->gettag('para');\n$p->beginsaving($t);\nif ( $p->gettrimmedtext('/para') =~ /interesting stuff/ ) {\n$p->restoresaved();\nprocesspara($p);\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "XML::TokeParser provides a procedural (\"pull mode\") interface to XML::Parser in much the same\nway that Gisle Aas' HTML::TokeParser provides a procedural interface to HTML::Parser.\nXML::TokeParser splits its XML input up into \"tokens,\" each corresponding to an XML::Parser\nevent.\n\nA token is a bless'd reference to an array whose first element is an event-type string and whose\nlast element is the literal text of the XML input that generated the event, with intermediate\nelements varying according to the event type.\n\nEach token is an *object* of type XML::TokeParser::Token. Read \"XML::TokeParser::Token\" to learn\nwhat methods are available for inspecting the token, and retrieving data from it.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "$p = XML::TokeParser->new($input, [options])\nCreates a new parser, specifying the input source and any options. If $input is a string, it\nis the name of the file to parse. If $input is a reference to a string, that string is the\nactual text to parse. If $input is a reference to a typeglob or an IO::Handle object\ncorresponding to an open file or socket, the text read from the handle will be parsed.\n\nOptions are name=>value pairs and can be any of the following:\n\nNamespaces\nIf set to a true value, namespace processing is enabled.\n\nParseParamEnt\nThis option is passed on to the underlying XML::Parser object; see that module's\ndocumentation for details.\n\nNoempty\nIf set to a true value, text tokens consisting of only whitespace (such as those created\nby indentation and line breaks in between tags) will be ignored.\n\nLatin\nIf set to a true value, all text other than the literal text elements of tokens will be\ntranslated into the ISO 8859-1 (Latin-1) character encoding rather than the normal UTF-8\nencoding.\n\nCatalog\nThe value is the URI of a catalog file used to resolve PUBLIC and SYSTEM identifiers.\nSee XML::Catalog for details.\n\n$token = $p->gettoken()\nReturns the next token, as an array reference, from the input. Returns undef if there are no\nremaining tokens.\n\n$p->ungettoken($token,...)\nPushes tokens back so they will be re-read. Useful if you've read one or more tokens too\nfar. Correctly handles \"partial\" tokens returned by gettag().\n\n$token = $p->gettag( [$token] )\nIf no argument given, skips tokens until the next start tag or end tag token. If an argument\nis given, skips tokens until the start tag or end tag (if the argument begins with '/') for\nthe named element. The returned token does not include an event type code; its first element\nis the element name, prefixed by a '/' if the token is for an end tag.\n\n$text = $p->gettext( [$token] )\nIf no argument given, returns the text at the current position, or an empty string if the\nnext token is not a 'T' token. If an argument is given, gathers up all text between the\ncurrent position and the specified start or end tag, stripping out any intervening tags\n(much like the way a typical Web browser deals with unknown tags).\n\n$text = $p->gettrimmedtext( [$token] )\nLike gettext(), but deletes any leading or trailing whitespaces and collapses multiple\nwhitespace (including newlines) into single spaces.\n\n$p->beginsaving( [$token] )\nCauses subsequent calls to gettoken(), gettag(), gettext(), and gettrimmedtext() to\nsave the returned tokens. In conjunction with restoresaved(), allows you to \"back up\"\nwithin a token stream. If an argument is supplied, it is placed at the beginning of the list\nof saved tokens (useful because you often won't know you want to begin saving until you've\nalready read the first token you want saved).\n\n$p->restoresaved()\nPushes all the tokens saved by beginsaving() back onto the token stream. Stops saving\ntokens. To cancel saving without backing up, call beginsaving() and restoresaved() in\nsuccession.\n\nXML::TokeParser::Token\nA token is a blessed array reference, that you acquire using \"$p->gettoken\" or \"$p->gettag\",\nand that might look like:\n\n[\"S\",  $tag, $attr, $attrseq, $raw]\n[\"E\",  $tag, $raw]\n[\"T\",  $text, $raw]\n[\"C\",  $text, $raw]\n[\"PI\", $target, $data, $raw]\n\nIf you don't like remembering array indices (you're a real programmer), you may access the\nattributes of a token like:\n\n\"$t->tag\", \"$t->attr\", \"$t->attrseq\", \"$t->raw\", \"$t->text\", \"$t->target\", \"$t->data\".\n\nPlease note that this may change in the future, where as there will be 4 token types,\nXML::TokeParser::Token::StartTag ....\n\nWhat kind of token is it?\n\nTo find out, inspect your token using any of these is* methods (1 == true, 0 == false, d'oh):\n\nistext\niscomment\nispi which is short for isprocessinstruction\nisstarttag\nisendtag\nistag\n\nWhat's that token made of? To retrieve data from your token, use any of the following methods,\ndepending on the kind of token you have:\n\ntarget\nonly for process instructions\n\ndata\nonly for process instructions\n\nraw for all tokens\n\nattr\nonly for start tags, returns a hashref ( \"print \"#link \", \"\"$t->attr\"\"->{href}\" ).\n\nmy $attrseq = $t->attrseq\nonly for start tags, returns an array ref of the keys found in \"$t->attr\" in the order they\noriginally appeared in.\n\nmy $tagname = $t->tag\nonly for tags ( \"print \"opening \", \"\"$t->tag\"\" if \"\"$t->isstarttag\" ).\n\nmy $text = $token->text\nonly for tokens of type text and comment\n\nHere's more detailed info about the tokens.\n\nStart tag\nThe token has five elements: 'S', the element's name, a reference to a hash of attribute\nvalues keyed by attribute names, a reference to an array of attribute names in the order in\nwhich they appeared in the tag, and the literal text.\n\nEnd tag\nThe token has three elements: 'E', the element's name, and the literal text.\n\nCharacter data (text)\nThe token has three elements: 'T', the parsed text, and the literal text. All contiguous\nruns of text are gathered into single tokens; there will never be two 'T' tokens in a row.\n\nComment\nThe token has three elements: 'C', the parsed text of the comment, and the literal text.\n\nProcessing instruction\nThe token has four elements: 'PI', the target, the data, and the literal text.\n\nThe literal text includes any markup delimiters (pointy brackets, <![CDATA[, etc.), entity\nreferences, and numeric character references and is in the XML document's original character\nencoding. All other text is in UTF-8 (unless the Latin option is set, in which case it's in\nISO-8859-1) regardless of the original encoding, and all entity and character references are\nexpanded.\n\nIf the Namespaces option is set, element and attribute names are prefixed by their (possibly\nempty) namespace URIs enclosed in curly brackets and xmlns:* attributes do not appear in 'S'\ntokens.\n\nDIFFERENCES FROM HTML::TokeParser\nUses a true XML parser rather than a modified HTML parser.\n\nText and comment tokens include extracted text as well as literal text.\n\nPI tokens include target and data as well as literal text.\n\nNo tokens for declarations.\n\nNo \"textify\" hash.\n\nungettoken correctly handles partial tokens returned by gettag().\n",
            "subsections": [
                {
                    "name": "begin_saving",
                    "content": ""
                }
            ]
        },
        "EXAMPLES": {
            "content": "Example:\n\nuse XML::TokeParser;\nuse strict;\n#\nmy $text = '<tag foo=\"bar\" foy=\"floy\"> some text <!--comment--></tag>';\nmy $p    = XML::TokeParser->new( \\$text );\n#\nprint $/;\n#\nwhile( defined( my $t = $p->gettoken() ) ){\nlocal $\\=\"\\n\";\nprint '         raw = ', $t->raw;\n#\nif( $t->tag ){\nprint '         tag = ', $t->tag;\n#\nif( $t->isstarttag ) {\nprint '        attr = ', join ',', %{$t->attr};\nprint '     attrseq = ', join ',', @{$t->attrseq};\n}\n#\nprint 'istag       ', $t->istag;\nprint 'isstarttag ', $t->isstarttag;\nprint 'isendtag   ', $t->isendtag;\n}\nelsif( $t->ispi ){\nprint '      target = ', $t->target;\nprint '        data = ', $t->data;\nprint 'ispi        ', $t->ispi;\n}\nelse {\nprint '        text = ', $t->text;\nprint 'istext      ', $t->istext;\nprint 'iscomment   ', $t->iscomment;\n}\n#\nprint $/;\n}\nEND\n\nOutput:\n\nraw = <tag foo=\"bar\" foy=\"floy\">\ntag = tag\nattr = foo,bar,foy,floy\nattrseq = foo,foy\nistag       1\nisstarttag 1\nisendtag   0\n\n\nraw =  some text\ntext =  some text\nistext      1\niscomment   0\n\n\nraw = <!--comment-->\ntext = comment\nistext      0\niscomment   1\n\n\nraw = </tag>\ntag = tag\nistag       1\nisstarttag 0\nisendtag   1\n",
            "subsections": []
        },
        "BUGS": {
            "content": "To report bugs, go to <http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-TokeParser> or send mail to\n<bug-XML-Tokeparser@rt.cpan.org>\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Copyright (c) 2003 D.H. aka PodMaster (current maintainer). Copyright (c) 2001 Eric Bohlman\n(original author).\n\nAll rights reserved. This program is free software; you can redistribute it and/or modify it\nunder the same terms as Perl itself. If you don't know what this means, visit <http://perl.com/>\nor <http://cpan.org/>.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "HTML::TokeParser, XML::Parser, XML::Catalog, XML::Smart, XML::Twig.\n",
            "subsections": []
        }
    },
    "summary": "XML::TokeParser - Simplified interface to XML::Parser",
    "flags": [],
    "examples": [
        "Example:",
        "use XML::TokeParser;",
        "use strict;",
        "my $text = '<tag foo=\"bar\" foy=\"floy\"> some text <!--comment--></tag>';",
        "my $p    = XML::TokeParser->new( \\$text );",
        "print $/;",
        "while( defined( my $t = $p->gettoken() ) ){",
        "local $\\=\"\\n\";",
        "print '         raw = ', $t->raw;",
        "if( $t->tag ){",
        "print '         tag = ', $t->tag;",
        "if( $t->isstarttag ) {",
        "print '        attr = ', join ',', %{$t->attr};",
        "print '     attrseq = ', join ',', @{$t->attrseq};",
        "print 'istag       ', $t->istag;",
        "print 'isstarttag ', $t->isstarttag;",
        "print 'isendtag   ', $t->isendtag;",
        "elsif( $t->ispi ){",
        "print '      target = ', $t->target;",
        "print '        data = ', $t->data;",
        "print 'ispi        ', $t->ispi;",
        "else {",
        "print '        text = ', $t->text;",
        "print 'istext      ', $t->istext;",
        "print 'iscomment   ', $t->iscomment;",
        "print $/;",
        "END",
        "Output:",
        "raw = <tag foo=\"bar\" foy=\"floy\">",
        "tag = tag",
        "attr = foo,bar,foy,floy",
        "attrseq = foo,foy",
        "istag       1",
        "isstarttag 1",
        "isendtag   0",
        "raw =  some text",
        "text =  some text",
        "istext      1",
        "iscomment   0",
        "raw = <!--comment-->",
        "text = comment",
        "istext      0",
        "iscomment   1",
        "raw = </tag>",
        "tag = tag",
        "istag       1",
        "isstarttag 0",
        "isendtag   1"
    ],
    "see_also": []
}