# node-jsxml

> an XML parser for javascript and node.js

Latest version **0.9.0** (published 2016-10-31) · 0 weekly downloads

## Install

```sh
npm install node-jsxml
pnpm add node-jsxml
yarn add node-jsxml
bun add node-jsxml
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.9.0 |
| Published | 2016-10-31 |
| First published | 2012-03-29 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.1.9 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Author | colorhook |
| Maintainers | colorhook |
| Keywords | xml, parser |

## Links

- npm: https://www.npmjs.com/package/node-jsxml
- Repository: https://github.com/colorhook/jsxml
- Issues: http://github.com/colorhook/jsxml/issues
- npm.io page: https://npm.io/package/node-jsxml

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.9.0 (latest) — 2016-10-31
- 0.8.0 — 2016-07-19
- 0.7.0 — 2015-12-01
- 0.6.0 — 2014-06-28
- 0.5.1 — 2013-11-04
- 0.5.0 — 2013-08-25
- 0.4.0 — 2013-05-22
- 0.3.0 — 2013-05-17
- 0.2.0 — 2012-03-29

## README

jsxml
==============

jsxml is an XML library for javascript (and node)

单元测试
------
[![travis build status](https://api.travis-ci.org/colorhook/jsxml.png)](https://www.travis-ci.org/colorhook/jsxml)


Install by NPM
--------------

```shell
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 &lt;script&gt; elements.

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

in Node, import using `require` function.

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

support AMD, CMD. Big thanks to [TimSchlechter](https://github.com/TimSchlechter).

```javascript
seajs.config({
    alias: {
        jsxml: '../src/jsxml.js'
    }
});
seajs.use('jsxml', function(jsxml){
    console.log(jsxml);
});
```

```javascript
var Namespace = jsxml.Namespace,
    QName = jsxml.QName,
    XML = jsxml.XML,
    XMLList = jsxml.XMLList;
```

Here you go:

```javascript
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

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

find all children by *

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

print the xml string

```javascript
console.log(xml.toXMLString());
```

modify namespace

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

find descendants nodes

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

get all children

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

get text node

```javascript
var text = xml.text();
```

get element node

```javascript
var elements = xml.elements();
```

get comment node

```javascript
var comments = xml.comments();
```

get attribute

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

get all attributes

```javascript
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:

```javascript
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:

```javascript
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

```javascript
var str = xml.toXMLString();
```

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

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

Advanced topics
----------------

You can also add, retrieve or remove namespaces:

```javascript
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: [colorhook@gmail.com]

License
-------

jsxml is free to use under MIT license.

---
_Source: https://npm.io/package/node-jsxml · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
