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

@oriun/x-men

v1.0.12

Published

XML parser

Downloads

3

Readme

X-men

Dependance-free XML parser

Features

  • XML parsing

Installation

Install with yarn or npm

  yarn add @oriun/x-men

Usage/Examples

We'll use this XML content as data for the examples below :

<note>
    <to>Tove</to>
    <to>Dany</to>
    <to>Kayle</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    <date type="unix">
        1639688607324
    </date>
</note>

First import the package and call the xml class to parse your content, then navigate into the tree as an object :

const Xmen = require('@oriun/x-men')

const data = "*** our xml content ***"

const { root } = new Xmen.xml(data)

const noteContent = root.note.body.$innerXML
// "Don't forget me this weekend!"

const recipients = root.note.to.$innerXML
// [ 'Tove', 'Dany', 'Kayle' ]

const date = root.note.date
// { $tagName: "date", $attributes: { "type": "unix"}, "$children": [...]}

let fullDate
if(date.type === "unix"){ // assertion ok
    fullDate = new Date(parseInt(date.$innerXML))
    // Date Thu Dec 16 2021 22:03:27 GMT+0100 ...
}

Note that the tree is immutable for now, next versions will add features for tree manipulation.

Properties

Considering the following XML:

<article id="42" type="blogpost">
    <title>Hello</title>
    <subtitle>world</subtitle>
</article>

$tagName

The tag name : article

$attributes

The tag attributes : { id: "42", type: "blogpost" }

$children

The children tags : [ { $tagName: "title",...}, { $tagName: "subtitle"}]

$innerXML

The content of the tag :

<title>Hello</title><subtitle>world</subtitle>

$outerXML

The content of the tag, including the tag itself :

<article id="42" type="blogpost"><title>Hello</title><subtitle>world</subtitle></article>

Naming issues

For ergonomy, navigating into the tree is simplified. For example root.note.to will first search for tag to in note.$children, then for property to in note.$attributes and finally for property to on the note object itself.

    const data = `
    <note children="todo">
        <children> A child list </children>
        <another> tag </another> 
    </note>`

    const { root } = new Xmen.xml(data)

    const children = root.note.children
    // "A child list"
    // Can't get the full list of child with this syntax

If your content may contain overlapping tags or attributes, consider using Xmen class specific properties : $tagName, $attributes, $children, $innerXML and $outerXML

    const children = root.note.$children
    /* [
        { $tagName: "children", ...},
        { $tagName: "another", ...}
    ] */

    const noteAttribute = root.note.$attributes.children
    // "todo"

License

MIT