xmerl_sax_parser(3erl)
π xmerl_sax_parser(3erl) β Erlang Module Definition π
okay so this is part 1 of 2, bestie β weβre doing the full tour, no skipping, no βlol Iβll read the rest laterβ energy. π
πΈ NAME πΈ
xmerl_sax_parser β XML SAX parser API β¨
π¦ DESCRIPTION π¦
Itβs a SAX parser for XML that sends the events through a callback interface. SAX is the Simple API for XML β originally a Java-only API, no cap. SAX was the first widely adopted API for XML in Java, and itβs a de facto standard where there are versions for several programming language environments other than Java. π«Ά
π DATA TYPES π
π§© option()
Options used to customize the behaviour of the parser. Possible options are:
{continuation_fun, ContinuationFun}βContinuationFunis a call back function to decide what to do if the parser runs into EOF before the document is complete. π{continuation_state, term()}β State that is accessible in the continuation call back function.{event_fun, EventFun}βEventFunis the call back function for parser events. π{event_state, term()}β State that is accessible in the event call back function.{file_type, FileType}β Flag that tells the parser if itβs parsing a DTD or a normal XML file (defaultnormal).FileType = normal | dtd
{encoding, Encoding}β Set default character set used (defaultUTF-8). This character set is used only if not explicitly given by the XML document.Encoding = utf8 | {utf16,big} | {utf16,little} | latin1 | list
skip_external_dtdβ Skips the external DTD during parsing. This option is the same as{external_entities, none}and{fail_undeclared_ref, false}but just for the DTD.disallow_entitiesβ Implies that parsing fails if anENTITYdeclaration is found.{entity_recurse_limit, N}β Sets how many levels of recursion that is allowed for entities. Default is 3 levels. π₯{external_entities, AllowedType}β Sets which types of external entities that should be allowed, if not allowed itβs just skipped.AllowedType = all | file | none
{fail_undeclared_ref, Boolean}β Decides how the parser should behave when an undeclared reference is found. Can be useful if one has turned off external entities so that an external DTD is not parsed. Default istrue.
π event()
The SAX events that are sent to the user via the callback.
startDocumentβ Receive notification of the beginning of a document. The SAX parser will send this event only once before any other event callbacks.endDocumentβ Receive notification of the end of a document. The SAX parser will send this event only once, and it will be the last event during the parse.{startPrefixMapping, Prefix, Uri}β Begin the scope of a prefix-URI Namespace mapping. Note that start/endPrefixMapping events are not guaranteed to be properly nested relative to each other: all startPrefixMapping events will occur immediately before the corresponding startElement event, and all endPrefixMapping events will occur immediately after the corresponding endElement event, but their order is not otherwise guaranteed. There will not be start/endPrefixMapping events for the"xml"prefix, since it is predeclared and immutable.Prefix = string()Uri = string()
{endPrefixMapping, Prefix}β End the scope of a prefix-URI mapping.Prefix = string()
{startElement, Uri, LocalName, QualifiedName, Attributes}β Receive notification of the beginning of an element. The Parser will send this event at the beginning of every element in the XML document; there will be a corresponding endElement event for every startElement event (even when the element is empty). All of the elementβs content will be reported, in order, before the corresponding endElement event.Uri = string()LocalName = string()QualifiedName = {Prefix, LocalName}Prefix = string()Attributes = [{Uri, Prefix, AttributeName, Value}]AttributeName = string()Value = string()
{endElement, Uri, LocalName, QualifiedName}β Receive notification of the end of an element. The SAX parser will send this event at the end of every element in the XML document; there will be a corresponding startElement event for every endElement event (even when the element is empty).Uri = string()LocalName = string()QualifiedName = {Prefix, LocalName}Prefix = string()
{characters, string()}β Receive notification of character data.{ignorableWhitespace, string()}β Receive notification of ignorable whitespace in element content.{processingInstruction, Target, Data}β Receive notification of a processing instruction. The Parser will send this event once for each processing instruction found: note that processing instructions may occur before or after the main document element.Target = string()Data = string()
{comment, string()}β Report an XML comment anywhere in the document (both inside and outside of the document element).startCDATAβ Report the start of a CDATA section. The contents of the CDATA section will be reported through the regular characters event.endCDATAβ Report the end of a CDATA section.{startDTD, Name, PublicId, SystemId}β Report the start of DTD declarations, itβs reporting the start of the DOCTYPE declaration. If the document has no DOCTYPE declaration, this event will not be sent.Name = string()PublicId = string()SystemId = string()
endDTDβ Report the end of DTD declarations, itβs reporting the end of the DOCTYPE declaration.{startEntity, SysId}β Report the beginning of some internal and external XML entities. ???{endEntity, SysId}β Report the end of an entity. ???{elementDecl, Name, Model}β Report an element type declaration. The content model will consist of the string"EMPTY", the string"ANY", or a parenthesised group, optionally followed by an occurrence indicator. The model will be normalized so that all parameter entities are fully resolved and all whitespace is removed, and will include the enclosing parentheses. Other normalization (such as removing redundant parentheses or simplifying occurrence indicators) is at the discretion of the parser.Name = string()Model = string()
{attributeDecl, ElementName, AttributeName, Type, Mode, Value}β Report an attribute type declaration.ElementName = string()AttributeName = string()Type = string()Mode = string()Value = string()
{internalEntityDecl, Name, Value}β Report an internal entity declaration.Name = string()Value = string()
{externalEntityDecl, Name, PublicId, SystemId}β Report a parsed external entity declaration.Name = string()PublicId = string()SystemId = string()
{unparsedEntityDecl, Name, PublicId, SystemId, Ndata}β Receive notification of an unparsed entity declaration event.Name = string()PublicId = string()SystemId = string()Ndata = string()
{notationDecl, Name, PublicId, SystemId}β Receive notification of a notation declaration event.Name = string()PublicId = string()SystemId = string()
π Other data types π
unicode_char()β Integer representing valid unicode codepoint.unicode_binary()β Binary with characters encoded in UTF-8 or UTF-16.latin1_binary()β Binary with characters encoded in iso-latin-1.
π EXPORTS π
π file(Filename, Options) -> Result π
Types:
Filename = string()
Options = [option()]
Result = {ok, EventState, Rest} |
{Tag, Location, Reason, EndTags, EventState}
Rest = unicode_binary() | latin1_binary()
Tag = atom() (fatal_error, or user defined tag)
Location = {CurrentLocation, EntityName, LineNo}
CurrentLocation = string()
EntityName = string()
LineNo = integer()
EventState = term()
Reason = term()
Parse file containing an XML document. This function uses a default continuation function to read the file in blocks. πβ¨
π stream(Xml, Options) -> Result π
Types:
Xml = unicode_binary() | latin1_binary() | [unicode_char()]
Options = [option()]
Result = {ok, EventState, Rest} |
{Tag, Location, Reason, EndTags, EventState}
Rest = unicode_binary() | latin1_binary() | [unicode_char()]
Tag = atom() (fatal_error or user defined tag)
Location = {CurrentLocation, EntityName, LineNo}
CurrentLocation = string()
EntityName = string()
LineNo = integer()
EventState = term()
Reason = term()
Parse a stream containing an XML document. π
π«Ά CALLBACK FUNCTIONS π«Ά
The callback interface is based on that the user sends a fun with the correct signature to the parser. π
Module:ContinuationFun(State) -> {NewBytes, NewState}
Okay bestie, this is the callback that feeds your parser when it's literally starving for bytes π Here's the type situation:
State = NewState = term()
NewBytes = binary() | list() (should be same as start input in stream/2)
So here's the vibe π this function gets called whenever the parser runs out of input data β think of it as the parser going "bestieβ¦ more? π₯Ί" β and it's your job to hand over the next chunk.
If your function can't get hold of more input, you return an empty list or an empty binary (which one depends on the start input you gave in stream/2, iykyk). That's your polite way of saying "that's all I've got β¨".
Any other types of errors are handled through exceptions, no cap. Use throw/1 to send this tuple:
{Tag = atom(), Reason = string()}
β¦if your continuation function hits a fatal error π. Tag is an atom that identifies the functional entity that sends the exception, and Reason is a string that describes the problem. So like, name yourself and spill the tea about what went wrong π«Ά
Module:EventFun(Event, Location, State) -> NewState
This one is the main character of the callbacks, fr fr π The types are giving very specific:
Event = event()
Location = {CurrentLocation, Entityname, LineNo}
CurrentLocation = string()
Entityname = string()
LineNo = integer()
State = NewState = term()
This function gets called for every single event the parser sends π Every one. Obsessed with how thorough that is, honestly.
Error handling? Done through exceptions, always has been π
Use throw/1 to send this tuple:
{Tag = atom(), Reason = string()}
β¦if your application encounters a fatal error π. Tag is an atom that identifies the functional entity that sends the exception, and Reason is a string that describes the problem. Same energy as before β tag yourself, then explain the drama β¨
Lowkey important: both of these callbacks communicate disasters via throw/1, not via return values. Return values are for the happy path only, bestie π¦
That's the whole page, babes! π This little masterpiece comes to you from Ericsson AB and it's the xmerl 1.3.31.1 era of the xmerl_sax_parser(3erl) manual page β go forth and parse slayfully πβ¨
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms