Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thedome/xmljs-sax
An xml parsing libary using sax.js
https://github.com/thedome/xmljs-sax
Last synced: about 2 months ago
JSON representation
An xml parsing libary using sax.js
- Host: GitHub
- URL: https://github.com/thedome/xmljs-sax
- Owner: TheDome
- License: other
- Created: 2020-06-14T04:32:16.000Z (over 4 years ago)
- Default Branch: develop
- Last Pushed: 2023-03-29T00:59:26.000Z (almost 2 years ago)
- Last Synced: 2024-12-16T14:18:46.743Z (about 2 months ago)
- Language: TypeScript
- Size: 1.97 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# XML-Sax
[![codecov](https://codecov.io/gh/TheDome/xmljs-sax/branch/develop/graph/badge.svg?token=BIEQR9H432)](https://codecov.io/gh/TheDome/xmljs-sax)
[![npm](https://img.shields.io/npm/v/saxxmlparser?color=red&label=version)](https://www.npmjs.com/package/saxxmlparser)
[![npm](https://img.shields.io/npm/dt/saxxmlparser)](https://www.npmjs.com/package/saxxmlparser)
[![GitHub commits since latest release (by SemVer)](https://img.shields.io/github/commits-since/thedome/xmljs-sax/latest)](https://github.com/TheDome/xmljs-sax/compare/master...develop)A small package to transverse xml into javascript
## Example
### Parse.ts
```typescript
import { readFileSync } from "fs";
import parse from "saxxmlparser";var xml = readFileSync("xml.xml");
parse(xml).then((node) => {
var price = node.resolveNSPath(
"A:envelope/A:body/B:getstockPriceResponsE/B:price",
{
A: "http://www.w3.org/2001/12/soap-envelope",
B: "http://www.example.org/stock",
}
);
console.log(price.value); // Prints 34.5
});
```### xml.xml
```xml
34.5
```
## Usage
### resolving a specific child
```typescript
import { readFileSync } from "fs";
import parse from "saxxmlparser";var xml = readFileSync("xml.xml");
parse(xml).then((node) => {
var price = node.resolveNSPath(
"A:envelope/A:body/B:getstockPriceResponsE/B:price", // Put the absolute path to the child in here
{
A: "http://www.w3.org/2001/12/soap-envelope",
B: "http://www.example.org/stock",
}
);
console.log(price.value); // Prints 34.5
});
```### Resolving a child whose parents don't matter
Sometimes when you only want a specific child but the parents or the path does not matter
```typescript
import { readFileSync } from "fs";
import parse from "saxxmlparser";var xml = readFileSync("xml.xml");
parse(xml).then((node) => {
// A double slash (//) in front of the search triggers a child search
var price = node.resolveNSPath(
"//B:price", // Put the absolute path to the child in here
{
A: "http://www.w3.org/2001/12/soap-envelope",
B: "http://www.example.org/stock",
}
);
console.log(price.value); // Prints 34.5
});
```### Resolving a child of a child whose parents don't matter
Sometimes when you only want a specific child but the parents or the path does not matter
```typescript
import { readFileSync } from "fs";
import parse from "saxxmlparser";var xml = readFileSync("xml.xml");
parse(xml).then((node) => {
// A double slash (//) in front of the search triggers a child search
var price = node.resolveNSPath(
"//B:GetStockPriceResponse/B:price", // Put the absolute path to the child in here
{
A: "http://www.w3.org/2001/12/soap-envelope",
B: "http://www.example.org/stock",
}
);
console.log(price.value); // Prints 34.5
});
```## Functions
### Resolver
Parses the supplied xml and returns the root node
If strict is set to true, the sax parser will be instructed to use strict mode (defaults to true)`parse(xml, strict = true): Promise`