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

node-jsxml

v0.9.0

Published

an XML parser for javascript and node.js

Downloads

2,274

Readme

jsxml

jsxml is an XML library for javascript (and node)

单元测试

travis build status

Install by NPM

npm install node-jsxml

How to use?

After add this library to your project, there will be a global object named jsxml.

in HTML file, import using <script> elements.

<script src="jsxml.js"></script>

in Node, import using require function.

const jsxml = require("node-jsxml");

support AMD, CMD. Big thanks to TimSchlechter.

seajs.config({
    alias: {
        jsxml: '../src/jsxml.js'
    }
});
seajs.use('jsxml', function(jsxml){
    console.log(jsxml);
});
var Namespace = jsxml.Namespace,
    QName = jsxml.QName,
    XML = jsxml.XML,
    XMLList = jsxml.XMLList;

Here you go:

var xml = new XML("<spring>" + 
		     "<list id='data'>" + 
		       "<element value='jsxml'/>" +
		       "<element value='is'/>" +
		       "<element value='an'/>" +
		       "<element value='xml'/>" +
		       "<element value='parser'/>" +
		     "</list>" +
            "</spring>");

find special childs

var child = xml.child('list');

find all children by *

var children = xml.child('*');

print the xml string

console.log(xml.toXMLString());

modify namespace

xml.addNamespace(new Namespace("ns", "http://colorhook.com"));
xml.children().addNamespace(new Namespace("prefix", "uri"));
console.log(xml.toXMLString());

find descendants nodes

var descendants = xml.descendants('element');

get all children

var children = xml.children();
//or
var children = xml.child('*');

get text node

var text = xml.text();

get element node

var elements = xml.elements();

get comment node

var comments = xml.comments();

get attribute

var attribute = xml.attribute("id");

get all attributes

var attributes = xml.attributes();

All methods above return an XML object or XMLList object, if you want to get the String type content, you should:

var xml = new XML(xmlContent);

var attrValue = xml.attribute('attrName').toString();
//or
var attrValue = xml.attribute('attrName').getValue();

var childA = xml.child('a').toString();
//or
var childA = xml.child('a').getValue();

If you want to modify the value, you should call method setValue:

var xml = new XML("your xml string");

var attr= xml.attribute('attrName');
attr.setValue("newValue");

var childA = xml.child('a');
childA.setValue("newValue");

You can regenerate the XML Content

var str = xml.toXMLString();

While dealing with a list of childs in XML tree, you should use XMLList API:

var list = xml.child("item");
list.each(function(item, index){
	//item is an XML
});

Advanced topics

You can also add, retrieve or remove namespaces:

var xml = new XML("your xml string");
var ns = xml.namespace("prefix");

var nsNew = new Namespace("prefix", 'uri');
xml.addNamespace(nsNew);
xml.removeNamespace(nsNew);

Bugs & Feedback

Please feel free report bugs or feature requests. You can send me private message on [github], or send me an email to: [[email protected]]

License

jsxml is free to use under MIT license.