Git Product home page Git Product logo

xmljs-sax's Introduction

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>

xmljs-sax's People

Contributors

dependabot[bot] avatar thedome avatar semantic-release-bot avatar

Stargazers

dillan teagle avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.