slimdom-sax-parser
Parses XML to a Slimdom document using Sax
Last updated 6 months ago by wvbe .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install slimdom-sax-parser 
SYNC missed versions from official npm registry.

slimdom-sax-parser

Parses XML to a slimdom document using saxes. slimdom is a fast, tiny, standards-compliant XML DOM implementation for browser and node.

  • Parses text, elements, attributes, processing instructions, comments and CDATA
  • Supports namespaces
  • Optionally track the position in XML string source
  • Works in NodeJS and the browser

Exports

  • sync (function) Synchronously return a slimdom Document for the given XML string.
  • slimdom (slimdom) A reference to the lib this parser is built around, as a convenience.
import { sync, slimdom } from 'slimdom-sax-parser';

Arguments

  • xml (string) The XML you want to parse as a string
  • options (object) Optional.
    • position (boolean) Set to true to track the position attribute on DOM nodes. Defaults to false.
    • additionalNamespaces (object) Prefix/URI mapping of namespaces that are not declared in XML
    • additionalEntities (object) Name/value mapping of entities. slimdom-sax-parser does not parse entity definitions from XML.
sync('<foo />', { position: false });

Returns

A slimdom Document instance.

const document = sync('<foo />');

Examples

Modify the XML DOM:

import { sync } from 'slimdom-sax-parser';

const document = sync(`<foo />`);

document.documentElement.setAttribute('bar', 'baz');
// document.documentElement.hasAttribute('bar') === true

Use with an XPath engine (fontoxpath):

import { sync } from 'slimdom-sax-parser';
import { evaluateXPath } from 'fontoxpath';

const document = sync(`<foo><bar /><baz /></foo>`);
const childElementsOfFoo = evaluateXPath('/foo/*', document);
// childElementsOfFoo.length === 2

Use source code position tracking:

import { slimdom, sync } from 'slimdom-sax-parser';

const xml = '<example><child-element /></example>';

const document = sync(xml, { position: true });
// document instanceof slimdom.Document === true

const childElement = document.documentElement.firstChild;
// childElement instanceof slimdom.Element === true

const position = childElement.position;
// xml.substring(position.start, position.end) === '<child-element />'

License

Copyright (c) 2019 Wybe Minnebo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Current Tags

  • 1.3.0                                ...           latest (6 months ago)

13 Versions

  • 1.3.0                                ...           6 months ago
  • 1.2.2                                ...           7 months ago
  • 1.2.1                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.3                                ...           2 years ago
  • 1.1.2                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           3 years ago
  • 1.0.0-rc4                                ...           3 years ago
  • 1.0.0-rc3                                ...           3 years ago
  • 1.0.0-rc2                                ...           3 years ago
  • 1.0.0-rc1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 2
This Month 2
Last Day 2
Last Week 0
Last Month 3
Dependencies (2)
Dev Dependencies (2)

Copyright 2014 - 2016 © taobao.org |