Coder Social home page Coder Social logo

jsxml's Introduction

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.

var 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 child nodes
var child = xml.child('list');

//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

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.

jsxml's People

Contributors

colorhook avatar baconmania avatar timschlechter avatar

Watchers

Ron Valstar avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.