NaturalIntelligence/fast-xml-parser

View on GitHub
docs/v4/7.PITags.md

Summary

Maintainability
Test Coverage
Processing Instructions are supported by Fast XML parser. However, they're treated as normal tags from the parsing perspective. Hence, the following properties are required to be set for attributes processing;

```js
ignoreAttributes: false,
allowBooleanAttributes: true
```

Here is an example;

Input
```xml
<?xml version="1.0"?>
<?textinfo whitespace is allowed ?>

<h1></h1>
```

Code to process PI tags
```js
const options = {
    ignoreAttributes: false,
    format: true,
    preserveOrder: true,
    allowBooleanAttributes: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);

// const builder = new XMLBuilder(options);
// const output = builder.build(result);
```
JS ordered object
```json
[
    {
        "?xml": [
            {
                "#text": ""
            }
        ],
        ":@": {
            "@_version": "1.0"
        }
    },
    {
        "?textinfo": [
            {
                "#text": ""
            }
        ],
        ":@": {
            "@_whitespace": true,
            "@_is": true,
            "@_allowed": true
        }
    },
    {
        "h1": []
    }
]
```

Please note that 
* `#text` is always empty to keep it consistent with other parsed properties.
* PI tag name starts with '?'
* attributes are parsed in the same way they are parsed for other normal tags

When you setup builder to parse the above JS ordered object
```js
const options = {
    ignoreAttributes: false,
    preserveOrder: true,
    allowBooleanAttributes: true,
    suppressBooleanAttributes: true
};
const builder = new XMLBuilder(options);
const output = builder.build(result);
```

Output
```xml
<?xml   version="1.0"?>
<?textinfo   whitespace is allowed?>
<h1></h1>
```