UNPKG

2.88 kBTypeScriptView Raw
1/**
2 * Contains the XmlParser class, which is a SAX parser using the easysax implementation.
3 */
4
5/**
6 * Specifies the type of parser event.
7 */
8export class ParserEventType {
9 /**
10 * Specifies the StartElement event type.
11 */
12 static StartElement: string;
13
14 /**
15 * Specifies the EndElement event type.
16 */
17 static EndElement: string;
18
19 /**
20 * Specifies the Text event type.
21 */
22 static Text: string;
23
24 /**
25 * Specifies the CDATA event type.
26 */
27 static CDATA: string;
28
29 /**
30 * Specifies the Comment event type.
31 */
32 static Comment: string;
33}
34
35/**
36 * Defines a position within string, in line and column form.
37 */
38export interface Position {
39 /**
40 * The line number. The first line is at index 1.
41 */
42 line: number;
43
44 /**
45 * The column number. The first character is at index 1.
46 */
47 column: number;
48}
49
50/**
51 * Provides information for a parser event.
52 */
53export interface ParserEvent {
54 /**
55 * Returns the type of the parser event. This is one of the ParserEventType static members.
56 */
57 eventType: string;
58
59 /**
60 * Get the position in the xml string where the event was generated.
61 */
62 position: Position;
63
64 /**
65 * If namespace processing is enabled, returns the prefix of the element in case the eventType is ParserEventType.StartElement or ParserEventType.EndElement.
66 */
67 prefix?: string;
68
69 /**
70 * If namespace processing is enabled, returns the namespace of the element in case the eventType is ParserEventType.StartElement or ParserEventType.EndElement.
71 */
72 namespace?: string;
73
74 /**
75 * Returns the name of the element in case the eventType is ParserEventType.StartElement or ParserEventType.EndElement.
76 */
77 elementName?: string;
78
79 /**
80 * Returns a JSON object with the attributes of an element in case the eventType is ParserEventType.StartElement.
81 */
82 attributes?: Object;
83
84 /**
85 * Returns the relevant data in case the eventType is ParserEventType.Text, ParserEventType.CDATA or ParserEventType.Comment.
86 */
87 data?: string;
88
89 /**
90 * Returns a JSON string representation of this instance.
91 */
92 toString(): string;
93}
94
95/**
96 * A simple non-validating SAX parser based on https://github.com/vflash/easysax version 0.1.14
97 */
98export class XmlParser {
99 /**
100 * Creates a new instance of the XmlParser class.
101 * @param onEvent The callback to execute when a parser event occurs. The 'event' parameter contains information about the event.
102 * @param onError The callback to execute when a parser error occurs. The 'error' parameter contains the error.
103 * @param processNamespaces Specifies whether namespaces should be processed.
104 */
105 constructor(onEvent: (event: ParserEvent) => void, onError?: (error: Error, position: Position) => void, processNamespaces?: boolean, angularSyntax?: boolean);
106
107 /**
108 * Parses the supplied xml string.
109 * @param xmlString The string containing the xml to parse.
110 */
111 parse(xmlString: string): void;
112}