back to expat
expat.Parser
generated from ../src/mod_expat/parser.cpp
Parser implements the expat XML parser.
More info on expat can be found at expat.sourceforge.net.
Constants
XML_STATUS
| Name |
Description |
| XML_STATUS_ERROR |
|
| XML_STATUS_OK |
|
Indicates the status of a document
Constructor
Parser
Parser(Encoding = null)
| Name |
Type |
Default |
Description |
| Encoding |
String |
null |
Specifies a character encoding to use for the document. This overrides the document encoding
declaration. There are four built-in encodings: US-ASCII, UTF-8, UTF-16, ISO-8859-1.
|
Constructs a new parser.
Properties
| Name |
Type |
Description |
characters  |
String |
When the onCharacter callback is not used, XMLParser will
collect the characters in this property. It will be cleared
when a start tag is read and it will be complete when an end
tag is read.
|
| onCharacter |
Function |
This function is called when contiguous text free of tags is found. Note that
this function can be called more then ounce for the same tag. The function
gets one argument: the received data.
|
| onComment |
Function |
Set a handler for comments. This function gets one argument: all text inside
the comment delimiters.
|
| onDefault |
Function |
The function hase one argument: any characters in the document which wouldn't otherwise be handled.
This includes both data for which no handlers can be set (like some kinds of DTD declarations)
and data which could be reported but which currently has no handler set. The characters are passed
exactly as they were present in the XML document except that they will be encoded in UTF-8.
Line boundaries are not normalized. Note that a byte order mark character is not
passed to the default handler. There are no guarantees about how characters are divided between
calls to the default handler: for example, a comment might be split between multiple calls.
Setting this handler has the side effect of turning off expansion of references to internally
defined general entities. Instead these references are passed to the default handler.
|
| onDefaultExpand |
Function |
This sets a default handler, but doesn't inhibit the expansion of internal entity references.
The entity reference will not be passed to the default handler. The function has one argument:
any characters in the document which wouldn't otherwise handled.
|
| onEndCData |
Function |
This function is called at the end of a CDATA section.
|
| onEndElement |
Function |
This function will be called everytime when a tag is ended (also for empty tags). The
functions gets one argument: the name of the element.
|
| onEndNamespaceDecl |
Function |
|
| onExternalEntityRef |
Function |
Set an external entity reference handler. This handler is also called for processing
an external DTD subset if parameter entity parsing is in effect. This function
gets 5 arguments: XMLParser, context, base, systemId and publicId. The function must
return an integer value: non-zero for success, zero for failure.
|
| onProcessing |
Function |
This function will be called for processing instructions. It gets two arguments: target and
data. A target is the first word in the processing instruction. The data is the rest of the
characters in it after skipping all whitespace after the initial word.
|
| onSkippedEntity |
Function |
The function has two arguments: the entity name and a boolean which will be true
the entity is a parameter entity and false for general entity.
This function is called in two situations:
- An entity reference is encountered for which no declaration has been read
and this is not an error.
- An internal entity reference is read, but not expanded,
because the onDefault handler has been called.
|
| onStartCData |
Function |
This function is called at the beginning of a CDATA section.
|
| onStartElement |
Function |
This function will be called everytime when a tag is started (also for empty tags). The
functions get two arguments: the name of the element and an object with contains
properties which you can access with the name of the attribute.
|
Methods
parse
parse(Content,
Final) : Integer
| Name |
Type |
Default |
Description |
| Content |
String |
|
Containing part (or perhaps all) of the document |
| Final |
Boolean |
|
The final parameter informs the parser that this
is the last piece of the document.
|
Parse some more of the document. Returns XML_STATUS_ERROR or XML_STATUS_OK.
(see XML_STATUS)
The following example shows how easy it is to create a simple DOM structure in JavaScript:
// Constructor for a XMLElement object
function XMLElement(parent, name, attrs)
{
this.parent = parent;
if ( parent != null )
parent.elements[parent.elements.length + 1] = this;
this.name = name;
this.attrs = attrs;
this.elements = new Array();
}
var p = new expat.Parser();
p.currentElement = null;
p.onStartElement = function(element, attrs)
{
this.currentElement = new XMLElement(this.currentElement, element, attrs);
}
p.onEndElement = function(element)
{
this.currentElement.data = this.characters;
if ( this.currentElement.parent != null )
this.currentElement = this.currentElement.parent;
}
p.parse('<tag1 attr="1" attr2="2"><child1>child</child1></tag1>', true);
// p.currentElement points to the root tag now(tag1)
reset
reset() : Boolean
Clean up the memory structures maintained by the parser so that it may be used again.
After this has been called, parser is ready to start parsing a new document