---
title: XMLParser
description: Event driven XML parser.
---

Event driven XML parser.

The XMLParser is an event driven XML parser that calls provided callback functions when it encounters elements to be parsed. It does not iself do any parsing.

## Properties

### didEndDocument

Function called when the parser ends parsing a document.

```typescript
didEndDocument: fn()
```

When the parser calls the function, it has successfully completed parsing the document.

### didEndElement

Function called when ended parsing an element.

```typescript
didEndElement: fn()
```

Called by the parser when it encounters an end tag for an element. The function takes the element name as a parameter.

### didStartDocument

Function called when the parser begins parsing a document.

```typescript
didStartDocument: fn()
```

### didStartElement

Function called when starting to parse an element.

```typescript
didStartElement: fn(string, {string: string})
```

Called by the parser when it encounters a start tag for an element. The function takes the element name as a parameter as well as a key value pair containing all the attributes associated with the element.

Use this function to update your state and prepare for receiving the characters of the element. After this function is called, the parser will call the foundCharacters callback function with all or parts of the characters of the element.

### foundCharacters

Function called when the parser finds characters of an element.

```typescript
foundCharacters: fn()
```

The parser calls this function with a string whenever it finds characters for the current element. This function may be called several times for a single element.

### parseErrorOccurred

Function called when the parser encounters an error.

```typescript
parseErrorOccurred: fn()
```

The parser will call this function when it encounters a fatal error preventing it from continuing to parse. When the function is called the parsing is stopped.

### string

XML string to be parsed.

```typescript
string: string
```

## Methods

<span id="-new-xmlparser" class="api-anchor"></span>

### new XMLParser(string)

Constructs an XMLParser.

```typescript
new XMLParser(string: string)
```

Constructs an event driven XML parser. It does not do any parsing on its own and therefore the callback functions must be set before starting to parse.

#### Parameters

**`string` `string`**

XML string to be parsed.

<span id="-parse" class="api-anchor"></span>

### parse()

Starts parsing.

```typescript
parse(): bool
```

Before calling this function you should ensure that the parser is correctly configured, i.e. the necessary callback functions should be set.

#### Return value

**`bool`**

Whether parsing was successfully started.