Skip to content

Commit

Permalink
meet nodejs and support doctype parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
colorhook committed Mar 29, 2012
1 parent 4da7ce0 commit 7905569
Show file tree
Hide file tree
Showing 7 changed files with 461 additions and 2,033 deletions.
117 changes: 61 additions & 56 deletions README.md
@@ -1,92 +1,97 @@
jsxml
==============

jsxml is an XML library for javascript
jsxml is an XML library for javascript (and node)

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

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

```javascript
var Namespace = jsxml.Namespace,
QName = jsxml.QName,
XML = jsxml.XML,
XMLList = jsxml.XMLList;
```
Here you go:
```javascript
var xml = new XML("your xml string");

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

//find child nodes
var child = xml.child('childTag');
//find child nodes
var child = xml.child('childTag');

//find descendants nodes
var descendants = xml.descendants('descendantTag');
//find descendants nodes
var descendants = xml.descendants('descendantTag');

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

//get text node
var text = xml.text();
//get text node
var text = xml.text();

//get element node
var elements = xml.elements();
//get element node
var elements = xml.elements();

//get comment node
var comments = xml.comments();
//get comment node
var comments = xml.comments();

//get attribute
var attribute = xml.attribute("attributeName");
//get attribute
var attribute = xml.attribute("attributeName");

//get all attributes
var attributes = xml.attributes();
//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:
```javascript
var xml = new XML("your xml string");

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

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

var childA = xml.child('a').getValue();
//or
var childA = xml.child('a').getValue();
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 xml = new XML("your xml string");
var attr= xml.attribute('attrName');
attr.setValue("newValue");

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

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

You can regenerate the XML

var str = xml.toXMLString();
```javascript
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
});
```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:

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

var nsNew = new Namespace("prefix", 'uri');
xml.addNamespace(nsNew);
xml.removeNamespace(nsNew);
```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
----------------
Expand Down
6 changes: 0 additions & 6 deletions build/jsxml-0.1.0-yui.js

This file was deleted.

0 comments on commit 7905569

Please sign in to comment.