{
    "mode": "perldoc",
    "parameter": "XML::Parser::Expat",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/XML%3A%3AParser%3A%3AExpat/json",
    "generated": "2026-06-13T22:59:55Z",
    "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 in expat, a single\ninstance of the parser can only parse one document. Calls to parsestring after the first for a\ngiven instance will die.\n\nExpat (and XML::Parser::Expat) are event based. As the parser recognizes parts of the document\n(say the start or end of an XML element), then any handlers registered for that type of an event\nare called with suitable parameters.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "new This is a class method, the constructor for XML::Parser::Expat. Options are passed as\nkeyword value pairs. The recognized options are:\n\n*   ProtocolEncoding\n\nThe protocol encoding name. The default is none. The expat built-in encodings are:\n\"UTF-8\", \"ISO-8859-1\", \"UTF-16\", and \"US-ASCII\". Other encodings may be used if they\nhave encoding maps in one of the directories in the @EncodingPath list. Setting the\nprotocol encoding overrides any encoding in the XML declaration.\n\n*   Namespaces\n\nWhen this option is given with a true value, then the parser does namespace processing.\nBy default, namespace processing is turned off. When it is turned on, the parser\nconsumes *xmlns* attributes and strips off prefixes from element and attributes names\nwhere those prefixes have a defined namespace. A name's namespace can be found using the\n\"namespace\" method and two names can be checked for absolute equality with the \"eqname\"\nmethod.\n\n*   NoExpand\n\nNormally, the parser will try to expand references to entities defined in the internal\nsubset. If this option is set to a true value, and a default handler is also set, then\nthe default handler will be called when an entity reference is seen in text. This has no\neffect if a default handler has not been registered, and it has no effect on the\nexpansion of entity references inside attribute values.\n\n*   StreamDelimiter\n\nThis option takes a string value. When this string is found alone on a line while\nparsing from a stream, then the parse is ended as if it saw an end of file. The intended\nuse is with a stream of xml documents in a MIME multipart format. The string should not\ncontain a trailing newline.\n\n*   ErrorContext\n\nWhen this option is defined, errors are reported in context. The value of ErrorContext\nshould be the number of lines to show on either side of the line in which the error\noccurred.\n\n*   ParseParamEnt\n\nUnless standalone is set to \"yes\" in the XML declaration, setting this to a true value\nallows the external DTD to be read, and parameter entities to be parsed and expanded.\n\n*   Base\n\nThe base to use for relative pathnames or URLs. This can also be done by using the base\nmethod.\n",
            "subsections": [
                {
                    "name": "setHandlers",
                    "content": "This method registers handlers for the various events. If no handlers are registered, then a\ncall to parsestring or parsefile will only determine if the corresponding XML document is\nwell formed (by returning without error.) This may be called from within a handler, after\nthe parse has started.\n\nSetting a handler to something that evaluates to false unsets that handler.\n\nThis method returns a list of type, handler pairs corresponding to the input. The handlers\nreturned are the ones that were in effect before the call to setHandlers.\n\nThe recognized events and the parameters passed to the corresponding handlers are:\n\n*   Start (Parser, Element [, Attr, Val [,...]])\n\nThis event is generated when an XML start tag is recognized. Parser is an\nXML::Parser::Expat instance. Element is the name of the XML element that is opened with\nthe start tag. The Attr & Val 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 that an XML empty tag\n(<foo/>) generates both a start and an end event.\n\nThere is always a lower level start and end handler installed that wrap the\ncorresponding callbacks. This is to handle the context mechanism. A consequence of this\nis that the default handler (see below) will not see a start tag or end tag unless the\ndefaultcurrent method is called.\n\n*   Char (Parser, String)\n\nThis event is generated when non-markup is recognized. The non-markup sequence of\ncharacters is in String. A single non-markup sequence of characters may generate\nmultiple calls to this handler. Whatever the encoding of the string in the original\ndocument, 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 recognized.\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 handler. This includes\nboth characters that are part of markup for which no events are generated (markup\ndeclarations) and characters that could generate events, but for which no handler has\nbeen registered.\n\nWhatever the encoding in the original document, the string is returned to the handler in\nUTF-8.\n\n*   Unparsed (Parser, Entity, Base, Sysid, Pubid, Notation)\n\nThis is called for a declaration of an unparsed entity. Entity is the name of the\nentity. Base is the base to be used for resolving a relative URI. Sysid is the system\nid. Pubid is the public 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 notation name. Base is the\nbase to be used for resolving a relative URI. Sysid is the system id. Pubid is the\npublic id. Base, 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 the base to be used for\nresolving a relative URI. Sysid is the system id. Pubid is the public id. Base, and\nPubid may be undefined.\n\nThis handler should either return a string, which represents the contents of the\nexternal entity, or return an open filehandle that can be read to obtain the contents of\nthe external entity, or 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 a glob (*FOO) or as a\nreference to a glob (e.g. an instance of IO::Handle).\n\n*   ExternEntFin (Parser)\n\nThis is called after an external entity has been parsed. It allows applications to\nperform cleanup on actions performed in the above ExternEnt handler.\n\n*   Entity (Parser, Name, Val, Sysid, Pubid, Ndata, IsParam)\n\nThis is called when an entity is declared. For internal entities, the Val parameter will\ncontain the value and the remaining three parameters will be undefined. For external\nentities, the Val parameter will be undefined, the Sysid parameter will have the system\nid, the Pubid parameter will have the public id if it was provided (it will be undefined\notherwise), the Ndata parameter will contain the notation for unparsed entities. If this\nis a parameter entity declaration, then the IsParam parameter is true.\n\nNote that this handler and the Unparsed handler above overlap. If both are set, then\nthis handler will not be called for unparsed entities.\n\n*   Element (Parser, Name, Model)\n\nThe element handler is called when an element declaration is found. Name is the element\nname, and Model is the content model as an XML::Parser::ContentModel object. See\n\"XML::Parser::ContentModel Methods\" for methods available for this class.\n\n*   Attlist (Parser, Elname, Attname, Type, Default, Fixed)\n\nThis handler is called for each attribute in an ATTLIST declaration. So an ATTLIST\ndeclaration that has multiple attributes will generate multiple calls to this handler.\nThe Elname parameter is the name of the element with which the attribute is being\nassociated. The Attname parameter is the name of the attribute. Type is the attribute\ntype, given as a string. Default is the default value, which will either be \"#REQUIRED\",\n\"#IMPLIED\" or a quoted string (i.e. the returned string will begin and end with a quote\ncharacter). If Fixed is true, then this is a fixed attribute.\n\n*   Doctype (Parser, Name, Sysid, Pubid, Internal)\n\nThis handler is called for DOCTYPE declarations. Name is the document type name. Sysid\nis the system id of the document type, if it was provided, otherwise it's undefined.\nPubid is the public id of the document type, which will be undefined if no public id was\ngiven. Internal will be true or false, indicating whether or not the doctype declaration\ncontains an internal subset.\n\n*   DoctypeFin (Parser)\n\nThis handler is called after parsing of the DOCTYPE declaration has finished, including\nany internal or external DTD declarations.\n\n*   XMLDecl (Parser, Version, Encoding, Standalone)\n\nThis handler is called for XML declarations. Version is a string containing the version.\nEncoding is either undefined or contains an encoding string. Standalone is either\nundefined, or true or false. Undefined indicates that no standalone parameter was given\nin the XML declaration. True or false indicates \"yes\" or \"no\" respectively.\n"
                },
                {
                    "name": "namespace",
                    "content": "Return the URI of the namespace that the name belongs to. If the name doesn't belong to any\nnamespace, an undef is returned. This is only valid on names received through the Start or\nEnd handlers from a single document, or through a call to the generatensname method. In\nother words, don't use names generated from one instance of XML::Parser::Expat with other\ninstances.\n"
                },
                {
                    "name": "eq_name",
                    "content": "Return true if name1 and name2 are identical (i.e. same name and from the same namespace.)\nThis is only meaningful if both names were obtained through the Start or End handlers from a\nsingle document, or through a call to the generatensname method.\n"
                },
                {
                    "name": "generate_ns_name",
                    "content": "Return a name, associated with a given namespace, good for using with the above 2 methods.\nThe namespace argument should be the namespace URI, not a prefix.\n\nnewnsprefixes\nWhen called from a start tag handler, returns namespace prefixes declared with this start\ntag. If called elsewhere (or if there were no namespace prefixes declared), it returns an\nempty list. Setting of the default namespace is indicated with '#default' as a prefix.\n"
                },
                {
                    "name": "expand_ns_prefix",
                    "content": "Return the uri to which the given prefix is currently bound. Returns undef if the prefix\nisn't currently bound. Use '#default' to find the current binding of the default namespace\n(if any).\n\ncurrentnsprefixes\nReturn a list of currently bound namespace prefixes. The order of the the prefixes in the\nlist has no meaning. If the default namespace is currently bound, '#default' appears in the\nlist.\n\nrecognizedstring\nReturns the string from the document that was recognized in order to call the current\nhandler. For instance, when called from a start handler, it will give us the start-tag\nstring. The string is encoded in UTF-8. This method doesn't return a meaningful string\ninside declaration handlers.\n\noriginalstring\nReturns the verbatim string from the document that was recognized in order to call the\ncurrent handler. The string is in the original document encoding. This method doesn't return\na meaningful string inside declaration handlers.\n\ndefaultcurrent\nWhen called from a handler, causes the sequence of characters that generated the\ncorresponding event to be sent to the default handler (if one is registered). Use of this\nmethod is deprecated in favor the recognizedstring method, which you can use without\ninstalling a default handler. This method doesn't deliver a meaningful string to the default\nhandler when called from inside declaration handlers.\n"
                },
                {
                    "name": "xpcroak",
                    "content": "Concatenate onto the given message the current line number within the XML document plus the\nmessage implied by ErrorContext. Then croak with the formed message.\n"
                },
                {
                    "name": "xpcarp",
                    "content": "Concatenate onto the given message the current line number within the XML document plus the\nmessage implied by ErrorContext. Then carp with 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"
                },
                {
                    "name": "base",
                    "content": "Returns the current value of the base for resolving relative URIs. If NEWBASE is supplied,\nchanges the base to that value.\n\ncontext\nReturns a list of element names that represent open elements, with the last one being the\ninnermost. Inside start and end tag handlers, this will be the tag of the parent element.\n\ncurrentelement\nReturns the name of the innermost currently opened element. Inside start or end handlers,\nreturns the parent of the element associated with those tags.\n"
                },
                {
                    "name": "in_element",
                    "content": "Returns true if NAME is equal to the name of the innermost currently opened element. If\nnamespace processing is being used and you want to check against a name that may be in a\nnamespace, then use the generatensname method to create the NAME argument.\n"
                },
                {
                    "name": "within_element",
                    "content": "Returns the number of times the given name appears in the context list. If namespace\nprocessing is being used and you want to check against a name that may be in a namespace,\nthen use the generatensname 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 current element. This will be\nzero outside of the root element. For example, this will return 1 when called from the start\nhandler for the root element start tag.\n"
                },
                {
                    "name": "skip_until",
                    "content": "INDEX is an integer that represents an element index. When this method is called, all\nhandlers are suspended until the start tag for an element that has an index number equal to\nINDEX is seen. If a start handler has been set, then this is the first tag that the start\nhandler will see after skipuntil has been called.\n"
                },
                {
                    "name": "position_in_context",
                    "content": "Returns a string that shows the current parse position. LINES should be an integer >= 0 that\nrepresents the number of lines on either side of the current parse line to place into the\nreturned string.\n"
                },
                {
                    "name": "xml_escape",
                    "content": "Returns TEXT with markup characters turned into character entities. Any additional\ncharacters provided as arguments are also turned into character references where found in\nTEXT.\n\nparse (SOURCE)\nThe SOURCE parameter should either be a string containing the whole XML document, or it\nshould be an open IO::Handle. Only a single document may be parsed for a given instance of\nXML::Parser::Expat, so this will croak if it's been called previously for this instance.\n"
                },
                {
                    "name": "parsestring",
                    "content": "Parses the given string as an XML document. Only a single document may be parsed for a given\ninstance of XML::Parser::Expat, so this will die if either parsestring or parsefile has been\ncalled for this instance previously.\n\nThis method is deprecated in favor of the parse method.\n"
                },
                {
                    "name": "parsefile",
                    "content": "Parses the XML document in the given file. Will die if parsestring or parsefile has been\ncalled previously for this instance.\n"
                },
                {
                    "name": "is_defaulted",
                    "content": "NO LONGER WORKS. To find out if an attribute is defaulted please use the specifiedattr\nmethod.\n\nspecifiedattr\nWhen the start handler receives lists of attributes and values, the non-defaulted (i.e.\nexplicitly specified) attributes occur in the list first. This method returns the number of\nspecified items in the list. So if this number is equal to the length of the list, there\nwere no defaulted values. Otherwise the number points to the index of the first defaulted\nattribute name.\n\nfinish\nUnsets all handlers (including internal ones that set context), but expat continues parsing\nto the end of the document or until it finds an error. It should finish up a lot faster than\nwith the handlers set.\n\nrelease\nThere are data structures used by XML::Parser::Expat that have circular references. This\nmeans that these structures will never be garbage collected unless these references are\nexplicitly broken. Calling this method breaks those references (and makes the instance\nunusable.)\n\nNormally, higher level calls handle this for you, but if you are using XML::Parser::Expat\ndirectly, then it's your responsibility to call it.\n\nXML::Parser::ContentModel Methods\nThe element declaration handlers are passed objects of this class as the content model of the\nelement declaration. They also represent content particles, components of a content model.\n\nWhen referred to as a string, these objects are automagicly converted to a string representation\nof 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 \"(#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 particles.\n\nisseq\nThis method returns true if the object is a sequence of content particles.\n\nquant\nThis method returns undef or a string representing the quantifier ('?', '*', '+') associated\nwith the model or particle.\n\nchildren\nThis method returns undef or (for mixed, choice, and sequence types) an array of component\ncontent particles. There will always be at least one component for choices and sequences,\nbut for a mixed content model of pure PCDATA, \"(#PCDATA)\", then an undef is returned.\n\nXML::Parser::ExpatNB Methods\nThe class XML::Parser::ExpatNB is a subclass of XML::Parser::Expat used for non-blocking access\nto the expat library. It does not support the parse, parsestring, or parsefile methods, but it\ndoes have these additional methods:\n"
                },
                {
                    "name": "parse_more",
                    "content": "Feed expat more text to munch on.\n\nparsedone\nTell expat that it's gotten the whole document.\n"
                }
            ]
        },
        "FUNCTIONS": {
            "content": "XML::Parser::Expat::loadencoding(ENCODING)\nLoad an external encoding. ENCODING is either the name of an encoding or the name of a file.\nThe basename is converted to lowercase and a '.enc' extension is appended unless there's one\nalready there. Then, unless it's an absolute pathname (i.e. begins with '/'), the first file\nby that name discovered in the @EncodingPath path list is used.\n\nThe encoding in the file is loaded and kept in the %EncodingTable table. Earlier encodings\nof the same name are replaced.\n\nThis function is automatically called by expat when it encounters an encoding it doesn't\nknow about. Expat shouldn't call this twice for the same encoding name. The only reason\nusers should use this function 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 for this version (2.x),\nprovided documentation, and added some standard package features.\n",
            "subsections": []
        }
    },
    "summary": "XML::Parser::Expat - Lowlevel access to James Clark's expat XML parser",
    "flags": [],
    "examples": [],
    "see_also": []
}