BPS Script API  2.24.4
bps::XmlReader Class Reference

A wrapper for QXmlStreamReader. More...

Public Types

enum  ErrorCode { NoError = 0 , UnexpectedElementError = 1 , CustomError = 2 , NotWellFormedError = 3 , PrematureEndOfDocumentError = 4 }
 Error codes. More...
 
enum  TokenType {
  NoToken = 0 , Invalid = 1 , StartDocument = 2 , EndDocument = 3 , StartElement = 4 , EndElement = 5 , Characters = 6 , Comment = 7 , DTD = 8 , EntityReference = 9 ,
  ProcessingInstruction = 10
}
 Token types. More...
 
enum  XmlType { XmlFile = 0 , XmlString = 1 }
 Constructor flags. More...
 

Public Member Functions

void raiseError (String aMessage='')
 Raises a custom error with an optional error message. More...
 
ByteArray readElementBytes ()
 Binary version of readElementText(), returning the decoded binary data. More...
 
String readElementText ()
 Convenience function to be called in case a StartElement was read. More...
 
TokenType readNext ()
 Reads the next token and returns its type. More...
 
 XmlReader (String aString, XmlType aType=XmlFile)
 Flexible constructor. More...
 

Properties

Boolean atEnd
 Returns true if the reader has read until the end of the XML document, or if an error has occurred and reading has been aborted. More...
 
ArrayOfObjects attributes
 The attributes of a StartElement, as array of objects where each object holds the data of one attribute. More...
 
Object attributeValues
 The attributes of a StartElement, as an object holding the qualified names of the attributes with their values as properties. More...
 
ByteArray bytes
 Returns the binary data from the text of Characters which is assumed to be Base64 encoded. More...
 
Number columnNumber
 The current column number. More...
 
String documentEncoding
 If tokenType is StartDocument, this function returns the encoding string as specified in the XML declaration. More...
 
String documentVersion
 If tokenType is StartDocument, this function returns the version string as specified in the XML declaration. More...
 
String dtdName
 If tokenType is DTD, this function returns the DTD's name. More...
 
String dtdPublicId
 If tokenType is DTD, this function returns the DTD's public identifier. More...
 
String dtdSystemId
 If tokenType is DTD, this function returns the DTD's system identifier. More...
 
ErrorCode error
 Returns the type of the current error, or NoError if no error occurred. More...
 
String errorString
 Returns the error message that was set with raiseError(). More...
 
Boolean hasError
 The reader has is in error state. More...
 
Boolean isCDATA
 Returns true if the reader reports characters that stem from a CDATA section. More...
 
Boolean isWhitespace
 The current token of type Characters is white space only. More...
 
Number lineNumber
 The current line number in the XML. More...
 
String name
 Returns the local name of a StartElement, EndElement, or an EntityReference. More...
 
String prefix
 Returns the local name of a StartElement, EndElement or an EntityReference. More...
 
String qualifiedName
 Returns the qualified name of a StartElement or EndElement. More...
 
String text
 Returns the text of Characters, Comment, DTD, or EntityReference. More...
 
String tokenString
 Returns the reader's current token as string. More...
 
TokenType tokenType
 Returns the type of the current token. More...
 

Detailed Description

A wrapper for QXmlStreamReader.

Example usage:

var xml = new bps.XmlReader('myfile.xml');
while (true) {
var tokenType = xml.readNext();
print(xml.tokenString);
if (xml.hasError) throw new Error(xml.errorString);
} // while
void print(...)
Print the expressions (applying toString() if necessary) to the console standard output (stdout),...
void importExtension(String aExtensionName)
Imports the named extension into the script engine.
A wrapper for QXmlStreamReader.
Definition: bps.XmlReader.js:20
@ EndDocument
The reader reports the end of the document.
Definition: bps.XmlReader.js:199
TokenType tokenType
Returns the type of the current token.
Definition: bps.XmlReader.js:170
The bps extension is a namespace assembling general BPS properties and functions.
Definition: bps.AsyncIO.js:1
@ Error
Some unspecified error happened.
Definition: bps.js:26

Member Enumeration Documentation

◆ ErrorCode

Error codes.

Enumerator
NoError 

No error has occurred.

UnexpectedElementError 

The parser encountered an element that was different to those it expected.

CustomError 

A custom error has been raised with raiseError().

NotWellFormedError 

The parser internally raised an error due to the read XML not being well-formed.

PrematureEndOfDocumentError 

The input stream ended before a well-formed XML document was parsed.

◆ TokenType

Token types.

Enumerator
NoToken 

The reader has not yet read anything.

Invalid 

An error has occurred, reported in error and errorString.

StartDocument 

The reader reports the XML version number in documentVersion, and the encoding as specified in the XML document in documentEncoding.

EndDocument 

The reader reports the end of the document.

StartElement 

The reader reports the start of an element with name. Empty elements are also reported as StartElement, followed directly by EndElement. The convenience function readElementText() can be called to concatenate all content until the corresponding EndElement. Attributes are reported in attributes and attributeValues.

EndElement 

The reader reports the end of an element with name.

Characters 

The reader reports characters in text. If the characters are all white-space, isWhitespace returns true. If the characters stem from a CDATA section, isCDATA is true.

Comment 

The reader reports a comment in text.

DTD 

The reader reports a DTD in text. Details of the DTD declaration are reported in in dtdName, dtdPublicId and dtdSystemId.

EntityReference 

The reader reports an entity reference that could not be resolved. The name of the reference is reported in name, the replacement in text.

ProcessingInstruction 

The reader reports a processing instruction.

◆ XmlType

Constructor flags.

Enumerator
XmlFile 

The given string is the name of an XML file to process.

XmlString 

The given string is the XML data to process.

Constructor & Destructor Documentation

◆ XmlReader()

bps::XmlReader::XmlReader ( String  aString,
XmlType  aType = XmlFile 
)

Flexible constructor.

Parameters
aStringThe file name or the XML itself.
aTypeWhether aString is a file name (the default), or the XML content itself.

Member Function Documentation

◆ raiseError()

void bps::XmlReader::raiseError ( String  aMessage = '')

Raises a custom error with an optional error message.

Parameters
aMessageThe custom message to apply if not empty.

◆ readElementBytes()

ByteArray bps::XmlReader::readElementBytes ( )

Binary version of readElementText(), returning the decoded binary data.

The text is assumed to be encoded as Base64.

Returns
The binary decoded element text, or empty array if the current token is not StartElement.

◆ readElementText()

String bps::XmlReader::readElementText ( )

Convenience function to be called in case a StartElement was read.

Reads until the corresponding EndElement and returns all text in-between. In case of no error, the current token (see tokenType) after having called this function is EndElement. The function concatenates text when it reads either Characters or EntityReference tokens, but skips ProcessingInstruction and Comment. In case anything else is read before reaching EndElement, the function returns what it read so far and raises an UnexpectedElementError.

Returns
The element text, or an empty string if the current token is not StartElement.

◆ readNext()

TokenType bps::XmlReader::readNext ( )

Reads the next token and returns its type.

Once an error is reported by readNext(), further reading of the XML stream is not possible. Then atEnd returns true, hasError returns true, and this function returns Invalid.

Returns
Type of the token read.

Property Documentation

◆ atEnd

Boolean bps::XmlReader::atEnd
read

Returns true if the reader has read until the end of the XML document, or if an error has occurred and reading has been aborted.

Otherwise, it returns false.

◆ attributes

ArrayOfObjects bps::XmlReader::attributes
read

The attributes of a StartElement, as array of objects where each object holds the data of one attribute.

Following elements are in the object:

NameTypeDescription
isDefaultBooleanTrue if the parser added this attribute with a default value following an ATTLIST declaration in the DTD.
nameStringThe attributes local name.
namespaceUriStringThe attributes resolved namespaceUri, or an empty string reference if the attribute does not have a defined namespace.
prefixStringThe attributes namespace prefix.
qualifiedNameStringThe attributes qualified name.
valueStringThe attributes value.

◆ attributeValues

Object bps::XmlReader::attributeValues
read

The attributes of a StartElement, as an object holding the qualified names of the attributes with their values as properties.

Example:

var val = xml.attributeValues.attrname;

Is equivalent to:

var val;
for (var a = 0; a < xml.attributes.length; ++a)
if (xml.attributes[a].qualifiedName == 'attrname')
val = attributes[a].value;
break;
}
ArrayOfObjects attributes
The attributes of a StartElement, as array of objects where each object holds the data of one attribu...
Definition: bps.XmlReader.js:41

◆ bytes

ByteArray bps::XmlReader::bytes
read

Returns the binary data from the text of Characters which is assumed to be Base64 encoded.

 

◆ columnNumber

Number bps::XmlReader::columnNumber
read

The current column number.

Column counting starts with 0.

◆ documentEncoding

String bps::XmlReader::documentEncoding
read

If tokenType is StartDocument, this function returns the encoding string as specified in the XML declaration.

Otherwise an empty string is returned.

◆ documentVersion

String bps::XmlReader::documentVersion
read

If tokenType is StartDocument, this function returns the version string as specified in the XML declaration.

Otherwise an empty string is returned.

◆ dtdName

String bps::XmlReader::dtdName
read

If tokenType is DTD, this function returns the DTD's name.

Otherwise an empty string is returned.

◆ dtdPublicId

String bps::XmlReader::dtdPublicId
read

If tokenType is DTD, this function returns the DTD's public identifier.

Otherwise an empty string is returned.

◆ dtdSystemId

String bps::XmlReader::dtdSystemId
read

If tokenType is DTD, this function returns the DTD's system identifier.

Otherwise an empty string is returned.

◆ error

ErrorCode bps::XmlReader::error
read

Returns the type of the current error, or NoError if no error occurred.

 

◆ errorString

String bps::XmlReader::errorString
read

Returns the error message that was set with raiseError().

 

◆ hasError

Boolean bps::XmlReader::hasError
read

The reader has is in error state.

Check error and errorString.

◆ isCDATA

Boolean bps::XmlReader::isCDATA
read

Returns true if the reader reports characters that stem from a CDATA section.

Otherwise returns false.

◆ isWhitespace

Boolean bps::XmlReader::isWhitespace
read

The current token of type Characters is white space only.

This is normally between two elements in formatted XML.

◆ lineNumber

Number bps::XmlReader::lineNumber
read

The current line number in the XML.

Line numbering starts with 1.

◆ name

String bps::XmlReader::name
read

Returns the local name of a StartElement, EndElement, or an EntityReference.

 

◆ prefix

String bps::XmlReader::prefix
read

Returns the local name of a StartElement, EndElement or an EntityReference.

 

◆ qualifiedName

String bps::XmlReader::qualifiedName
read

Returns the qualified name of a StartElement or EndElement.

A qualified name is the raw name of an element in the XML data. It consists of the namespace prefix, followed by colon, followed by the element's local name. Since the namespace prefix is not unique (the same prefix can point to different namespaces and different prefixes can point to the same namespace), you shouldn't use qualifiedName(), but the resolved namespaceUri() and the attributes local name.

◆ text

String bps::XmlReader::text
read

Returns the text of Characters, Comment, DTD, or EntityReference.

 

◆ tokenString

String bps::XmlReader::tokenString
read

Returns the reader's current token as string.

 

◆ tokenType

TokenType bps::XmlReader::tokenType
read

Returns the type of the current token.

 


The documentation for this class was generated from the following file: