\n\
</note>\n\
<note noteName="My second note">\n\
<to>To\n\
<from>From\n\
<date timezone="UTC">Fri, 10 Jul 2015 09:00:00\n\
<subject>Note subject\n\
<body>Note body\n\
</note>\n\
</notes>\n';
var rootElement;
var characterData = [];
var elementStack = [];
parser.startElementHandler = function(name, attrs) {
var data = attrs; // use attrs object with all properties as template
data.name = name; // add the name to the object
if(!rootElement) { // the first element we see is the root element we want to send as response
rootElement = data;
} else {
var currentElement = elementStack[elementStack.length - 1];
if(!currentElement.children) { // first time we see a child we have to create the children array
currentElement.children = [ data ];
} else {
currentElement.children.push(data)
}
}
elementStack.push(data);
};
parser.endElementHandler = function(name) {
elementStack.pop();
};
parser.characterDataHandler = function(s) {
var currentElement = elementStack[elementStack.length - 1];
if (!currentElement.characterData) { // the first time we see char data we store it as string
currentElement.characterData = s;
} else if (!Array.isArray(currentElement.characterData)) { // if we already have a string we convert it to an array and append the new data
currentElement.characterData = [currentElement.characterData, s];
} else { // just append new data to the existing array
currentElement.characterData.push(s);
}
};
parser.parse(xml);
$.response.contentType = "application/json";
$.response.setBody(JSON.stringify({
rootElement: rootElement,
characterData: characterData
}));
-
attlistDeclHandler :function
-
This property holds a callback function with the following parameters: elname(String), attname(String), att_type(String), dflt(String) and isrequired(Integer).
Type:
Example
parser.attlistDeclHandler = function(elname, attname, att_type, dflt, isrequired) {
// do some thing
}
-
characterDataHandler :function
-
This property holds a callback function with one parameter: s(String).
Type:
Example
parser.characterDataHandler = function(s) {
// do some thing
}
-
-
This property holds a callback function with one parameter: data(String).
Type:
Example
parser.commentHandler = function(data) {
// do some thing
}
-
currentByteIndex :Integer
-
This property holds the current byte index of the parser
Type:
-
currentColumnNumber :Integer
-
This property holds the current column position of the parser
Type:
-
currentLineNumber :Integer
-
This property holds the current line position of the parser
Type:
-
endCDataSectionHandler :function
-
This property holds a callback function with no parameters.
Type:
Example
parser.endCDataSectionHandler = function() {
// do some thing
}
-
endDoctypeDeclHandler :function
-
This property holds a callback function with no parameters.
Type:
Example
parser.endDoctypeDeclHandler = function() {
// do some thing
}
-
endElementHandler :function
-
This property holds a callback function with one parameter: name(String).
Type:
Example
parser.endElementHandler = function(name) {
// do some thing
}
-
endNameSpaceDeclHandler :function
-
This property holds a callback function with one parameter: prefix(String).
Type:
Example
parser.endNameSpaceDeclHandler = function(prefix) {
// do some thing
}
-
entityDeclHandler :function
-
This property holds a callback function with seven parameters: entityName(String), is_parameter_entity(Integer), value(String), systemId(String), publicId(String) and notationName(String).
NOTE: During parsing it is the application's responsibility to prevent large number of multiple XML entity expansions.
Type:
Example
parser.entityDeclHandler = function(entityName, is_parameter_entity, value, systemId, publicId, notationName) {
// do some thing
}
-
externalEntityRefHandler :function
-
This property holds a callback function with four parameters: context(String), systemId(String) and publicId(String).
Type:
Example
parser.externalEntityRefHandler = function(context, systemId, publicId) {
// do some thing
}
-
notationDeclHandler :function
-
This property holds a callback function with four parameters: notationName(String), systemId(String) and publicId(String).
Type:
Example
parser.notationDeclHandler = function(notationName, systemId, publicId) {
// do some thing
}
-
processingInstructionHandler :function
-
This property holds a callback function with two parameters: target(String) and data(String).
Type:
Example
parser.processingInstructionHandler = function(target, data) {
// do some thing
}
-
startCDataSectionHandler :function
-
This property holds a callback function with no parameters.
Type:
Example
parser.startCDataSectionHandler = function() {
// do some thing
}
-
startDoctypeDeclHandler :function
-
This property holds a callback function with four parameters: doctypeName(String), sysid(String), pubid(String) and has_internal_subset(Integer).
Type:
Example
parser.startDoctypeDeclHandler = function(doctypeName, sysid, pubid, has_internal_subset) {
// do some thing
}
-
startElementHandler :function
-
This property holds a callback function with two parameters: name(String) and atts(Object).
atts contains key:value pairs of the attributes.
Type:
Example
parser.startElementHandler = function(name, atts) {
// do some thing
}
-
startNameSpaceDeclHandler :function
-
This property holds a callback function with two parameters: prefix(String) and uri(String).
Type:
Example
parser.startNameSpaceDeclHandler = function(prefix, uri) {
// do some thing
}
-
xmlDeclHandler :function
-
This property holds a callback function with three parameters: version(String), encoding(String), standalone(Integer).
Type:
Example
parser.xmlDeclHandler = function(version, encoding, standalone) {
// do some thing
}
-
parse(xml)
-
This method parses the xmlString.
Parameters:
Name |
Type |
Description |
xml |
String
|
data that should be parsed |
Throws:
Throws an error if there was a problem during parsing or
the parser was stopped without the possibility of resuming
Example
var xmlString = //Some xml
var parser = new $.util.SAXParser()
//... set handlers
parser.parse(xmlString);
-
parse(xml, encoding)
-
This method parses the xmlArrayBuffer.
Parameters:
Name |
Type |
Argument |
Description |
xml |
ArrayBuffer
|
|
data that should be parsed |
encoding |
String
|
<optional>
|
supported encodings are UTF-8, UTF-16 and US-ASCII. The default is UTF-8. |
Throws:
Throws an error if there was a problem during parsing or
the parser was stopped without the possibility of resuming
Example
var xmlArrayBuffer = //Some xml
var parser = new $.util.SAXParser()
//... set handlers
parser.parse(xmlArrayBuffer);
-
parse(xml, encoding)
-
This method parses the content of a $.web.Body object.
Parameters:
Name |
Type |
Argument |
Description |
xml |
$.web.Body
|
|
data that should be parsed. The body should contain the XML. Otherwise the parser throws an error. |
encoding |
String
|
<optional>
|
supported encodings are UTF-8, UTF-16 and US-ASCII. The default is UTF-8. |
Throws:
Throws an error if there was a problem during parsing or
the parser was stopped without the possibility of resuming
Example
var body = $.request.body
var parser = new $.util.SAXParser()
//... set handlers
parser.parse(body);
-
reset()
-
This method resets the parser.
Example
var parser = new $.util.SAXParser()
// ...
parser.parse(xmlString1);
parser.reset();
parser.parse(xmlString2);
-
resume()
-
This method resumes the parser. It should not be called from a callback function.
Example
var parser = new $.util.SAXParser()
parser.startElementHandler = function(name, atts) {
//... some logic here
parser.stop(true);
//...
}
parser.parse(xmlString);
parser.startElementHandler = function(name, atts) {
//... some other logic here
}
parser.resume();
-
stop(isResumable)
-
This method stops the parser. It should be called from a callback function.
Parameters:
Name |
Type |
Description |
isResumable |
Boolean
|
if true than when called the parser is suspended and can be resumed.
If false the parser cannot be resumed and an error is thrown. |
Throws:
Throws an error if isResumable = false
Example
var parser = new $.util.SAXParser()
parser.startElementHandler = function(name, atts) {
//... some logic here
parser.stop(false);
//...
}
try {
parser.parse(xmlString);
} catch(error) {
// handle parser.stop(false) error
}