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

trixml

v0.1.2

Published

A trivally easy-to-use XML manipulator

Downloads

14

Readme

Trivial XML

Simple parsing and building of XML documents, powered by new features only available in ES6.

The goals of this project:

  • To make XML parsing require little-to-no error checking. Exploring the node tree should be as forgiving as possible.
  • To provide an easy way to open, modify and save an XML document in a single, unified library.
  • To be easily installed without any native code compilation.
  • To provide a novel and terse API by taking advantage of the latest ES6 metaprogramming features.
  • Useful for quick and dirty jobs which don't need fine grained control over every intricacy of XML.
  • Prioritising ease-of-use over speed.
  • To make you re-think your approach to XML parsing.

Requires a recent version of Node which has Proxy support.

Examples

Parsing an XML document

var trixml = require('trixml');
var xml = trixml.parseSync(`
    <?xml version="1.0"?>
    <Root an="attribute" another="one">
        <Envelope>
            <Message id="msg1">Hello</Message>
            <Message id="msg2">World</Message>
            <Message id="msg3">!</Message>
        </Envelope>
    </Root>
`);

xml.attr() // {an: "attribute", another: "one"}
xml.name; // "Root"
xml.value; // ""
xml.Envelope.Message.value; // "Hello"
xml.Envelope.Message.map(msg => msg.value).join(' ') // "Hello World !"
xml.Envelope.Message.attr('id') // msg1
xml.Envelope.Message.map(msg => msg.attr('id')).join(',') // "msg1,msg2,msg3"

Building an XML document

var trixml = require('trixml');
var xml = trixml.newDoc('root');

xml.Header("Hello ...");
xml.Body.Message("World", {length: 5});
xml.Body.Message("World!", {length: 6, planet: "Earth"}); // overwrites value & merges attributes
xml.Footer.wibble({a: "b", c: "d"});
xml.Footer.wobble("Easy!");
xml.Footer.addChild("wubble", {e: "f"}, "Simple!");
xml.Footer.addChild("wubble");
xml.Footer.flub("fluub");
xml.Trailer;

console.log(xml.toXML());

// <?xml version="1.0"?>
// <root>
//     <Header>Hello ...</Header>
//     <Body>
//         <Message length="6" planet="Earth">World!</Message>
//     </Body>
//     <Footer>
//         <wibble a="b" c="d" />
//         <wobble>Easy!</wobble>
//         <wubble e="f">Simple</wubble>
//         <wubble />
//         <flub>fluub</flub>
//     </Footer>
//     <Trailer />
// </root>

A basic RSS parser

var trixml = require('trixml');
var https = require('https');

function getRSSXML(callback) {
    var xmlString = "";
    https.request({
        hostname: 'github.com',
        path: '/mikuso.atom'
    }, res => {
        res.on('data', (d) => {
            xmlString += d;
        });
        res.once('end', () => callback(xmlString));
    }).end();
}

getRSSXML((xmlString) => {
    var doc = trixml.parseSync(xmlString);

    var items = doc.entry.map(e => `${e.published}: ${e.title}`).join("\n");

    console.log(`Title: ${doc.title}\nUpdated: ${doc.updated}\n------\n${items}`);

    // Title: mikuso’s Activity
    // Updated: 2016-07-22T17:21:22Z
    // ------
    // 2016-07-22T17:21:22Z: mikuso pushed to master at mikuso/trixml
    // 2016-07-22T17:19:48Z: mikuso pushed to master at mikuso/trixml
    // 2016-07-22T17:18:00Z: mikuso pushed to master at mikuso/trixml
    // 2016-07-22T17:18:00Z: mikuso created tag v0.0.3 at mikuso/trixml
    // ...
});

Gotchas

It's possible for nodes in your XML document to have names which conflict with the names of members of the XMLNode or XMLNodeCollection objects. When this happens, you cannot use the shorthand method for accessing child nodes.

The following XML nodes are affected: <inspect>, <toJSON>, <toString>, <value>, <children>, <comments>, <attr>, <addChild>, <get>, <cloneSync>, <remove>, <empty>

In these cases, you must use the longhand method like this:

var root = trixml.parseSync(`
    <?xml version="1.0"?>
    <root>
        <inspect>
            <addChild>gotcha</addChild>
        </inspect>
    </root>
`);

root.get('inspect').get('addChild').value === "gotcha"; // true

// don't do this... (it won't work)
root.inspect.addChild.value; // TypeError: Cannot read property 'addChild' of null

Todo:

  • Add support for streams
  • Async parsing, returning Promises
  • Ability to remove attributes
  • Add support for more Array methods on XMLNodeCollection
  • More tests