{
    "mode": "perldoc",
    "parameter": "XML::Parser::Expat",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/XML%3A%3AParser%3A%3AExpat/json",
    "generated": "2026-05-30T12:39:24Z",
    "synopsis": "use XML::Parser::Expat;\n$parser = XML::Parser::Expat->new;\n$parser->setHandlers('Start' => \\&sh,\n'End'   => \\&eh,\n'Char'  => \\&ch);\nopen(my $fh, '<', 'info.xml') or die \"Couldn't open\";\n$parser->parse($fh);\nclose($fh);\n# $parser->parse('<foo id=\"me\"> here <em>we</em> go </foo>');\nsub sh\n{\nmy ($p, $el, %atts) = @;\n$p->setHandlers('Char' => \\&spec)\nif ($el eq 'special');\n...\n}\nsub eh\n{\nmy ($p, $el) = @;\n$p->setHandlers('Char' => \\&ch)  # Special elements won't contain\nif ($el eq 'special');         # other special elements\n...\n}",
    "sections": {
        "NAME": {
            "content": "XML::Parser::Expat - Lowlevel access to James Clark's expat XML parser\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use XML::Parser::Expat;\n\n$parser = XML::Parser::Expat->new;\n$parser->setHandlers('Start' => \\&sh,\n'End'   => \\&eh,\n'Char'  => \\&ch);\nopen(my $fh, '<', 'info.xml') or die \"Couldn't open\";\n$parser->parse($fh);\nclose($fh);\n# $parser->parse('<foo id=\"me\"> here <em>we</em> go </foo>');\n\nsub sh\n{\nmy ($p, $el, %atts) = @;\n$p->setHandlers('Char' => \\&spec)\nif ($el eq 'special');\n...\n}\n\nsub eh\n{\nmy ($p, $el) = @;\n$p->setHandlers('Char' => \\&ch)  # Special elements won't contain\nif ($el eq 'special');         # other special elements\n...\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module provides an interface to James Clark's XML parser, expat. As\nin expat, a single instance of the parser can only parse one document.\nCalls to parsestring after the first for a given instance will die.\n\nExpat (and XML::Parser::Expat) are event based. As the parser recognizes\nparts of the document (say the start or end of an XML element), then any\nhandlers registered for that type of an event are called with suitable\nparameters.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "new This is a class method, the constructor for XML::Parser::Expat.\nOptions are passed as keyword value pairs. The recognized options\nare:\n\n*   ProtocolEncoding\n\nThe protocol encoding name. The default is none. The expat\nbuilt-in encodings are: \"UTF-8\", \"ISO-8859-1\", \"UTF-16\", and\n\"US-ASCII\". Other encodings may be used if they have encoding\nmaps in one of the directories in the @EncodingPath list.\nSetting the protocol encoding overrides any encoding in the XML\ndeclaration.\n\n*   Namespaces\n\nWhen this option is given with a true value, then the parser\ndoes namespace processing. By default, namespace processing is\nturned off. When it is turned on, the parser consumes *xmlns*\nattributes and strips off prefixes from element and attributes\nnames where those prefixes have a defined namespace. A name's\nnamespace can be found using the \"namespace\" method and two\nnames can be checked for absolute equality with the \"eqname\"\nmethod.\n\n*   NoExpand\n\nNormally, the parser will try to expand references to entities\ndefined in the internal subset. If this option is set to a true\nvalue, and a default handler is also set, then the default\nhandler will be called when an entity reference is seen in text.\nThis has no effect if a default handler has not been registered,\nand it has no effect on the expansion of entity references\ninside attribute values.\n\n*   StreamDelimiter\n\nThis option takes a string value. When this string is found\nalone on a line while parsing from a stream, then the parse is\nended as if it saw an end of file. The intended use is with a\nstream of xml documents in a MIME multipart format. The string\nshould not contain a trailing newline.\n\n*   ErrorContext\n\nWhen this option is defined, errors are reported in context. The\nvalue of ErrorContext should be the number of lines to show on\neither side of the line in which the error occurred.\n\n*   ParseParamEnt\n\nUnless standalone is set to \"yes\" in the XML declaration,\nsetting this to a true value allows the external DTD to be read,\nand parameter entities to be parsed and expanded.\n\n*   Base\n\nThe base to use for relative pathnames or URLs. This can also be\ndone by using the base method.\n\nsetHandlers(TYPE, HANDLER [, TYPE, HANDLER [...]])\nThis method registers handlers for the various events. If no\nhandlers are registered, then a call to parsestring or parsefile\nwill only determine if the corresponding XML document is well formed\n(by returning without error.) This may be called from within a\nhandler, after the parse has started.\n\nSetting a handler to something that evaluates to false unsets that\nhandler.\n\nThis method returns a list of type, handler pairs corresponding to\nthe input. The handlers returned are the ones that were in effect\nbefore the call to setHandlers.\n\nThe recognized events and the parameters passed to the corresponding\nhandlers are:\n\n*   Start (Parser, Element [, Attr, Val [,...]])\n\nThis event is generated when an XML start tag is recognized.\nParser is an XML::Parser::Expat instance. Element is the name of\nthe XML element that is opened with the start tag. The Attr &\nVal pairs are generated for each attribute in the start tag.\n\n*   End (Parser, Element)\n\nThis event is generated when an XML end tag is recognized. Note\nthat an XML empty tag (<foo/>) generates both a start and an end\nevent.\n\nThere is always a lower level start and end handler installed\nthat wrap the corresponding callbacks. This is to handle the\ncontext mechanism. A consequence of this is that the default\nhandler (see below) will not see a start tag or end tag unless\nthe defaultcurrent method is called.\n\n*   Char (Parser, String)\n\nThis event is generated when non-markup is recognized. The\nnon-markup sequence of characters is in String. A single\nnon-markup sequence of characters may generate multiple calls to\nthis handler. Whatever the encoding of the string in the\noriginal document, this is given to the handler in UTF-8.\n\n*   Proc (Parser, Target, Data)\n\nThis event is generated when a processing instruction is\nrecognized.\n\n*   Comment (Parser, String)\n\nThis event is generated when a comment is recognized.\n\n*   CdataStart (Parser)\n\nThis is called at the start of a CDATA section.\n\n*   CdataEnd (Parser)\n\nThis is called at the end of a CDATA section.\n\n*   Default (Parser, String)\n\nThis is called for any characters that don't have a registered\nhandler. This includes both characters that are part of markup\nfor which no events are generated (markup declarations) and\ncharacters that could generate events, but for which no handler\nhas been registered.\n\nWhatever the encoding in the original document, the string is\nreturned to the handler in UTF-8.\n\n*   Unparsed (Parser, Entity, Base, Sysid, Pubid, Notation)\n\nThis is called for a declaration of an unparsed entity. Entity\nis the name of the entity. Base is the base to be used for\nresolving a relative URI. Sysid is the system id. Pubid is the\npublic id. Notation is the notation name. Base and Pubid may be\nundefined.\n\n*   Notation (Parser, Notation, Base, Sysid, Pubid)\n\nThis is called for a declaration of notation. Notation is the\nnotation name. Base is the base to be used for resolving a\nrelative URI. Sysid is the system id. Pubid is the public id.\nBase, Sysid, and Pubid may all be undefined.\n\n*   ExternEnt (Parser, Base, Sysid, Pubid)\n\nThis is called when an external entity is referenced. Base is\nthe base to be used for resolving a relative URI. Sysid is the\nsystem id. Pubid is the public id. Base, and Pubid may be\nundefined.\n\nThis handler should either return a string, which represents the\ncontents of the external entity, or return an open filehandle\nthat can be read to obtain the contents of the external entity,\nor return undef, which indicates the external entity couldn't be\nfound and will generate a parse error.\n\nIf an open filehandle is returned, it must be returned as either\na glob (*FOO) or as a reference to a glob (e.g. an instance of\nIO::Handle).\n\n*   ExternEntFin (Parser)\n\nThis is called after an external entity has been parsed. It\nallows applications to perform cleanup on actions performed in\nthe above ExternEnt handler.\n\n*   Entity (Parser, Name, Val, Sysid, Pubid, Ndata, IsParam)\n\nThis is called when an entity is declared. For internal\nentities, the Val parameter will contain the value and the\nremaining three parameters will be undefined. For external\nentities, the Val parameter will be undefined, the Sysid\nparameter will have the system id, the Pubid parameter will have\nthe public id if it was provided (it will be undefined\notherwise), the Ndata parameter will contain the notation for\nunparsed entities. If this is a parameter entity declaration,\nthen the IsParam parameter is true.\n\nNote that this handler and the Unparsed handler above overlap.\nIf both are set, then this handler will not be called for\nunparsed entities.\n\n*   Element (Parser, Name, Model)\n\nThe element handler is called when an element declaration is\nfound. Name is the element name, and Model is the content model\nas an XML::Parser::ContentModel object. See\n\"XML::Parser::ContentModel Methods\" for methods available for\nthis class.\n\n*   Attlist (Parser, Elname, Attname, Type, Default, Fixed)\n\nThis handler is called for each attribute in an ATTLIST\ndeclaration. So an ATTLIST declaration that has multiple\nattributes will generate multiple calls to this handler. The\nElname parameter is the name of the element with which the\nattribute is being associated. The Attname parameter is the name\nof the attribute. Type is the attribute type, given as a string.\nDefault is the default value, which will either be \"#REQUIRED\",\n\"#IMPLIED\" or a quoted string (i.e. the returned string will\nbegin and end with a quote character). If Fixed is true, then\nthis is a fixed attribute.\n\n*   Doctype (Parser, Name, Sysid, Pubid, Internal)\n\nThis handler is called for DOCTYPE declarations. Name is the\ndocument type name. Sysid is the system id of the document type,\nif it was provided, otherwise it's undefined. Pubid is the\npublic id of the document type, which will be undefined if no\npublic id was given. Internal will be true or false, indicating\nwhether or not the doctype declaration contains an internal\nsubset.\n\n*   DoctypeFin (Parser)\n\nThis handler is called after parsing of the DOCTYPE declaration\nhas finished, including any internal or external DTD\ndeclarations.\n\n*   XMLDecl (Parser, Version, Encoding, Standalone)\n\nThis handler is called for XML declarations. Version is a string\ncontaining the version. Encoding is either undefined or contains\nan encoding string. Standalone is either undefined, or true or\nfalse. Undefined indicates that no standalone parameter was\ngiven in the XML declaration. True or false indicates \"yes\" or\n\"no\" respectively.\n\nnamespace(name)\nReturn the URI of the namespace that the name belongs to. If the\nname doesn't belong to any namespace, an undef is returned. This is\nonly valid on names received through the Start or End handlers from\na single document, or through a call to the generatensname method.\nIn other words, don't use names generated from one instance of\nXML::Parser::Expat with other instances.\n\neqname(name1, name2)\nReturn true if name1 and name2 are identical (i.e. same name and\nfrom the same namespace.) This is only meaningful if both names were\nobtained through the Start or End handlers from a single document,\nor through a call to the generatensname method.\n\ngeneratensname(name, namespace)\nReturn a name, associated with a given namespace, good for using\nwith the above 2 methods. The namespace argument should be the\nnamespace URI, not a prefix.\n\nnewnsprefixes\nWhen called from a start tag handler, returns namespace prefixes\ndeclared with this start tag. If called elsewhere (or if there were\nno namespace prefixes declared), it returns an empty list. Setting\nof the default namespace is indicated with '#default' as a prefix.\n\nexpandnsprefix(prefix)\nReturn the uri to which the given prefix is currently bound. Returns\nundef if the prefix isn't currently bound. Use '#default' to find\nthe current binding of the default namespace (if any).\n\ncurrentnsprefixes\nReturn a list of currently bound namespace prefixes. The order of\nthe the prefixes in the list has no meaning. If the default\nnamespace is currently bound, '#default' appears in the list.\n\nrecognizedstring\nReturns the string from the document that was recognized in order to\ncall the current handler. For instance, when called from a start\nhandler, it will give us the start-tag string. The string is encoded\nin UTF-8. This method doesn't return a meaningful string inside\ndeclaration handlers.\n\noriginalstring\nReturns the verbatim string from the document that was recognized in\norder to call the current handler. The string is in the original\ndocument encoding. This method doesn't return a meaningful string\ninside declaration handlers.\n\ndefaultcurrent\nWhen called from a handler, causes the sequence of characters that\ngenerated the corresponding event to be sent to the default handler\n(if one is registered). Use of this method is deprecated in favor\nthe recognizedstring method, which you can use without installing a\ndefault handler. This method doesn't deliver a meaningful string to\nthe default handler when called from inside declaration handlers.\n\nxpcroak(message)\nConcatenate onto the given message the current line number within\nthe XML document plus the message implied by ErrorContext. Then\ncroak with the formed message.\n\nxpcarp(message)\nConcatenate onto the given message the current line number within\nthe XML document plus the message implied by ErrorContext. Then carp\nwith the formed message.\n\ncurrentline\nReturns the line number of the current position of the parse.\n\ncurrentcolumn\nReturns the column number of the current position of the parse.\n\ncurrentbyte\nReturns the current position of the parse.\n\nbase([NEWBASE]);\nReturns the current value of the base for resolving relative URIs.\nIf NEWBASE is supplied, changes the base to that value.\n\ncontext\nReturns a list of element names that represent open elements, with\nthe last one being the innermost. Inside start and end tag handlers,\nthis will be the tag of the parent element.\n\ncurrentelement\nReturns the name of the innermost currently opened element. Inside\nstart or end handlers, returns the parent of the element associated\nwith those tags.\n\ninelement(NAME)\nReturns true if NAME is equal to the name of the innermost currently\nopened element. If namespace processing is being used and you want\nto check against a name that may be in a namespace, then use the\ngeneratensname method to create the NAME argument.\n\nwithinelement(NAME)\nReturns the number of times the given name appears in the context\nlist. If namespace processing is being used and you want to check\nagainst a name that may be in a namespace, then use the\ngeneratensname method to create the NAME argument.\n\ndepth\nReturns the size of the context list.\n\nelementindex\nReturns an integer that is the depth-first visit order of the\ncurrent element. This will be zero outside of the root element. For\nexample, this will return 1 when called from the start handler for\nthe root element start tag.\n\nskipuntil(INDEX)\nINDEX is an integer that represents an element index. When this\nmethod is called, all handlers are suspended until the start tag for\nan element that has an index number equal to INDEX is seen. If a\nstart handler has been set, then this is the first tag that the\nstart handler will see after skipuntil has been called.\n\npositionincontext(LINES)\nReturns a string that shows the current parse position. LINES should\nbe an integer >= 0 that represents the number of lines on either\nside of the current parse line to place into the returned string.\n\nxmlescape(TEXT [, CHAR [, CHAR ...]])\nReturns TEXT with markup characters turned into character entities.\nAny additional characters provided as arguments are also turned into\ncharacter references where found in TEXT.\n\nparse (SOURCE)\nThe SOURCE parameter should either be a string containing the whole\nXML document, or it should be an open IO::Handle. Only a single\ndocument may be parsed for a given instance of XML::Parser::Expat,\nso this will croak if it's been called previously for this instance.\n\nparsestring(XMLDOCSTRING)\nParses the given string as an XML document. Only a single document\nmay be parsed for a given instance of XML::Parser::Expat, so this\nwill die if either parsestring or parsefile has been called for this\ninstance previously.\n\nThis method is deprecated in favor of the parse method.\n\nparsefile(FILENAME)\nParses the XML document in the given file. Will die if parsestring\nor parsefile has been called previously for this instance.\n\nisdefaulted(ATTNAME)\nNO LONGER WORKS. To find out if an attribute is defaulted please use\nthe specifiedattr method.\n\nspecifiedattr\nWhen the start handler receives lists of attributes and values, the\nnon-defaulted (i.e. explicitly specified) attributes occur in the\nlist first. This method returns the number of specified items in the\nlist. So if this number is equal to the length of the list, there\nwere no defaulted values. Otherwise the number points to the index\nof the first defaulted attribute name.\n\nfinish\nUnsets all handlers (including internal ones that set context), but\nexpat continues parsing to the end of the document or until it finds\nan error. It should finish up a lot faster than with the handlers\nset.\n\nrelease\nThere are data structures used by XML::Parser::Expat that have\ncircular references. This means that these structures will never be\ngarbage collected unless these references are explicitly broken.\nCalling this method breaks those references (and makes the instance\nunusable.)\n\nNormally, higher level calls handle this for you, but if you are\nusing XML::Parser::Expat directly, then it's your responsibility to\ncall it.\n\nXML::Parser::ContentModel Methods\nThe element declaration handlers are passed objects of this class as the\ncontent model of the element declaration. They also represent content\nparticles, components of a content model.\n\nWhen referred to as a string, these objects are automagicly converted to\na string representation of the model (or content particle).\n\nisempty\nThis method returns true if the object is \"EMPTY\", false otherwise.\n\nisany\nThis method returns true if the object is \"ANY\", false otherwise.\n\nismixed\nThis method returns true if the object is \"(#PCDATA)\" or\n\"(#PCDATA|...)*\", false otherwise.\n\nisname\nThis method returns if the object is an element name.\n\nischoice\nThis method returns true if the object is a choice of content\nparticles.\n\nisseq\nThis method returns true if the object is a sequence of content\nparticles.\n\nquant\nThis method returns undef or a string representing the quantifier\n('?', '*', '+') associated with the model or particle.\n\nchildren\nThis method returns undef or (for mixed, choice, and sequence types)\nan array of component content particles. There will always be at\nleast one component for choices and sequences, but for a mixed\ncontent model of pure PCDATA, \"(#PCDATA)\", then an undef is\nreturned.\n\nXML::Parser::ExpatNB Methods\nThe class XML::Parser::ExpatNB is a subclass of XML::Parser::Expat used\nfor non-blocking access to the expat library. It does not support the\nparse, parsestring, or parsefile methods, but it does have these\nadditional methods:\n\nparsemore(DATA)\nFeed expat more text to munch on.\n\nparsedone\nTell expat that it's gotten the whole document.\n",
            "subsections": []
        },
        "FUNCTIONS": {
            "content": "XML::Parser::Expat::loadencoding(ENCODING)\nLoad an external encoding. ENCODING is either the name of an\nencoding or the name of a file. The basename is converted to\nlowercase and a '.enc' extension is appended unless there's one\nalready there. Then, unless it's an absolute pathname (i.e. begins\nwith '/'), the first file by that name discovered in the\n@EncodingPath path list is used.\n\nThe encoding in the file is loaded and kept in the %EncodingTable\ntable. Earlier encodings of the same name are replaced.\n\nThis function is automatically called by expat when it encounters an\nencoding it doesn't know about. Expat shouldn't call this twice for\nthe same encoding name. The only reason users should use this\nfunction is to explicitly load an encoding not contained in the\n@EncodingPath list.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Larry Wall <larry@wall.org> wrote version 1.0.\n\nClark Cooper <coopercc@netheaven.com> picked up support, changed the API\nfor this version (2.x), provided documentation, and added some standard\npackage features.\n",
            "subsections": []
        }
    },
    "summary": "XML::Parser::Expat - Lowlevel access to James Clark's expat XML parser",
    "flags": [],
    "examples": [],
    "see_also": []
}