# @oriun/x-men

> XML parser

Latest version **1.0.12** (published 2022-08-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @oriun/x-men
pnpm add @oriun/x-men
yarn add @oriun/x-men
bun add @oriun/x-men
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.12 |
| Published | 2022-08-21 |
| First published | 2021-12-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | oriun |
| Keywords | xml, parser, xml2js, xml2json |

## Links

- npm: https://www.npmjs.com/package/@oriun/x-men
- Repository: https://github.com/Oriun/x-men
- Homepage: https://github.com/Oriun/x-men#readme
- Issues: https://github.com/Oriun/x-men/issues
- npm.io page: https://npm.io/package/@oriun/x-men

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.0.12 (latest) — 2022-08-21
- 1.0.11 — 2022-03-25
- 1.0.10 — 2022-03-25
- 1.0.9 — 2022-03-25
- 1.0.8 — 2022-03-25
- 1.0.7 — 2022-03-25
- 1.0.6 — 2021-12-24
- 1.0.5 — 2021-12-17
- 1.0.3 — 2021-12-16
- 1.0.2 — 2021-12-16
- 1.0.1 — 2021-12-16
- 1.0.0 — 2021-12-16

## README

# X-men

Dependance-free XML parser


## Features

- XML parsing




## Installation

Install with yarn or npm

```bash
  yarn add @oriun/x-men
```
    
## Usage/Examples

We'll use this XML content as `data` for the examples below :
```xml
<note>
    <to>Tove</to>
    <to>Dany</to>
    <to>Kayle</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    <date type="unix">
        1639688607324
    </date>
</note>
```

First import the package and call the `xml` class to parse your content, then navigate into the tree as an object :

```javascript
const Xmen = require('@oriun/x-men')

const data = "*** our xml content ***"

const { root } = new Xmen.xml(data)

const noteContent = root.note.body.$innerXML
// "Don't forget me this weekend!"

const recipients = root.note.to.$innerXML
// [ 'Tove', 'Dany', 'Kayle' ]

const date = root.note.date
// { $tagName: "date", $attributes: { "type": "unix"}, "$children": [...]}

let fullDate
if(date.type === "unix"){ // assertion ok
    fullDate = new Date(parseInt(date.$innerXML))
    // Date Thu Dec 16 2021 22:03:27 GMT+0100 ...
}
```
Note that the tree is immutable for now, next versions will add features for tree manipulation.


## Properties
Considering the following XML:
```xml
<article id="42" type="blogpost">
    <title>Hello</title>
    <subtitle>world</subtitle>
</article>
```
#### $tagName
The tag name : `article`
#### $attributes
The tag attributes : `{ id: "42", type: "blogpost" }`
#### $children
The children tags : `[ { $tagName: "title",...}, { $tagName: "subtitle"}]`
#### $innerXML
The content of the tag : 
```xml
<title>Hello</title><subtitle>world</subtitle>
```
#### $outerXML
The content of the tag, including the tag itself : 
```xml
<article id="42" type="blogpost"><title>Hello</title><subtitle>world</subtitle></article>
```


## Naming issues
For ergonomy, navigating into the tree is simplified. For example `root.note.to` will first search for tag `to` in `note.$children`, then for property `to` in `note.$attributes` and finally for property `to` on the `note` object itself.
```javascript
    const data = `
    <note children="todo">
        <children> A child list </children>
        <another> tag </another> 
    </note>`

    const { root } = new Xmen.xml(data)

    const children = root.note.children
    // "A child list"
    // Can't get the full list of child with this syntax
```

If your content may contain overlapping tags or attributes, consider using Xmen class specific properties : `$tagName, $attributes, $children, $innerXML and $outerXML`
```javascript
    const children = root.note.$children
    /* [
        { $tagName: "children", ...},
        { $tagName: "another", ...}
    ] */

    const noteAttribute = root.note.$attributes.children
    // "todo"
```
## License

[MIT](https://choosealicense.com/licenses/mit/)

---
_Source: https://npm.io/package/@oriun/x-men · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
