1.4.2 • Published 2 years ago

saxxmlparser v1.4.2

Weekly downloads
4
License
Apache-2.0
Repository
github
Last release
2 years ago

XML-Sax

codecov npm npm GitHub commits since latest release (by SemVer)

A small package to transverse xml into javascript

Example

Parse.ts

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 version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

  <soap:Body xmlns:m="http://www.example.org/stock">
    <m:GetStockPriceResponse>
      <m:Price>34.5</m:Price>
    </m:GetStockPriceResponse>
  </soap:Body>

</soap:Envelope>

Usage

resolving a specific child

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

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

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<XmlNode>

1.4.2

2 years ago

1.3.6

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.5

2 years ago

1.3.4

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago