XmlParser
XmlParser
The XmlParser
class presents a simple way to parse data from an XML content.
One scenario where you could use this class is when a service returns an XML response.
With this class you can go through the XML code and search for a specific attribute and its value or to take the data(e.g.
text value) locked between the XML elements.
The XmlParser
constructor function has the following shape: XmlParser(onEvent: (event: ParserEvent) => void, onError?: (error: Error, position: Position) => void, processNamespaces?: boolean, angularSyntax?: boolean)
Where:
-
onEvent
is the callback to execute when a parser event occurs. Theevent
parameter contains information about the event. -
onError
is the callback to execute when a parser error occurs. The 'error' parameter contains the error. -
processNamespaces
specifies whether namespaces should be processed.
Below is a StackBlitz IDE with an example of how to use the XmlParser
class.
In order to try it out and see the results of the code, you have to download the Nativescript Preview
app from App Store and/or Google Play.
Once you have the app on your phone, use the phone Camera to scan the QR code in the Preview tab.
include::partial$ns-core-xml-parser-1.adoc
Method(s)
Name | Return Type | Description |
---|---|---|
|
|
Parses the supplied xml string.
+ |
ParserEventType class
From this class you get static properties that represent the various types of parser events.
Position
Defines a position within a string, in line and column form.
Name | Type | Description |
---|---|---|
|
|
The line number. The first line is at index 1. |
|
|
The column number. The first character is at index 1. |
ParserEvent data
The ParserEvent
object provides information about a parser event, and it has the following properties:
Name | Type | Description |
---|---|---|
|
|
Returns the type of the parser event. This is one of the ParserEventType static members. |
|
|
Get the position in the XML string where the event was generated. |
|
|
If namespace processing is enabled, returns the prefix of the element in case the eventType is |
|
|
If namespace processing is enabled, returns the namespace of the element in case the eventType is |
|
|
If namespace processing is enabled, returns the namespace of the element in case the eventType is |
|
|
Returns a JSON object with the attributes of an element in case the eventType is |
|
|
Returns the relevant data in case the eventType is |
|
|
Returns a JSON string representation of this instance. |