npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xmljsonkit

v1.0.4

Published

XML to JSON parser library with ordered elements

Downloads

2

Readme

xmljsonkit

XML to JSON parser library with ordered elements.

xmljsonkit is a XML to JSON parser library that is built on the fast and extensible Chevrotain parser.

xmljsonkit is a small extenion of the Chevrotain Example XML Parser with a JSON Concrete Syntax Tree (CST) visitor that can be extended for special XML syntax or JSON handling.

Values that are simple strings will be converted to numbers, if possible. The conversion process is configurable.

Simple string text values will be promoted from objects to string property values for more convinient JSON handling.

  "<genre>Computer</genre>" => { genre:"Computer" }
  "<price>  44.95  </price>" => { price: 44.95 }

There are two parsers:

  1. xml2json which does simple JSON object translation.
  2. orderedxml2json which extends xml2json for maintaining order of elements.

The XML Lexer, Parser, and JSON Visitor are all easily extended or changed to meet your exact JSON translation requirements.

Ordered XML Parsing

Sometimes order of elements is critical, for example with the XML represents a graphical format with serialized operations that must be executed in order.

For example, these shapes or stencils have path commands which must be executed in order. Other xml2json libraries do NOT maintain order, but xmljsonkit orderedxml2json does maintain order.

orderedxml2json takes a list of property names that require ordered array handling, and only builds the __elements ordered array for that elements contents.

<shapes>
	<shape name="or" aspect="variable">
		<background>
			<path>
				<move x="0" y="0"/>
				<quad x1="100" y1="0" x2="100" y2="50"/>
				<quad x1="100" y1="100" x2="0" y2="100"/>
				<close/>
			</path>
		</background>
		<foreground>
			<fillstroke/>
		</foreground>
	</shape>
</shapes>

Creates JSON with the correct order in a reserved array property called __elements.

    const ORDERD_PROPERTY = "__elements";
    var shapes = result.shapes.shape as any[];
    var shape = shapes[0];
    expect(shape._name).toBe("or");
    expect(shape.background).toBeDefined();
    expect(shape.background.path).toBeDefined();
    expect(shape.background.path.__elements).toBeDefined();
    var path = shape.background.path.__elements as any[];
    expect(path.length).toBe(4);
    expect(path[0]).toEqual({move: { _x:0, _y:0}});
    expect(path[1]).toEqual({quad: { _x1:100, _y1:0, _x2:100, _y2:50}});
    expect(path[2]).toEqual({quad: { _x1:100, _y1:100, _x2:0, _y2:100}});
    expect(path[3]).toEqual({close: {}});

Emedded text between elements is also preserved.

<catalog>
  <book id="bk101">
Before author<author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>  44.95  </price>after price<publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications
with XML.</description>Last</book>

Maintains the correct order of text and XML elements.

    const ORDERD_PROPERTY = "__elements";
    const books = result.catalog.book;
    expect(books.length).toBe(2);
    var book = books[0];
    expect(book._id).toBe("bk101");
    expect(book.__elements).toBeDefined();
    var bookProps:any[] = book.__elements;
    expect(bookProps.length).toBe(9);
    expect(bookProps[0]["#text"]).toBe("Before author");
    expect(bookProps[1].author).toBe("Gambardella, Matthew");
    expect(bookProps[4].price).toBe(44.95);
    expect(bookProps[5]["#text"]).toBe("after price");
    expect(bookProps[6].publish_date).toBe("2000-10-01");
    expect(bookProps[8]["#text"]).toBe("Last");