Comparing version 0.3.0 to 0.3.1
@@ -68,2 +68,6 @@ (function () { | ||
XmlElement.prototype._error = function(err) { | ||
throw err; | ||
}; | ||
// Useful functions | ||
@@ -81,3 +85,3 @@ | ||
} | ||
return null; | ||
return undefined; | ||
}; | ||
@@ -101,3 +105,3 @@ | ||
} | ||
return null; | ||
return undefined; | ||
}; | ||
@@ -124,3 +128,3 @@ | ||
else | ||
return null; | ||
return undefined; | ||
}; | ||
@@ -142,3 +146,3 @@ | ||
var finalVal = this.val.trim().replace(/</g, "<").replace(/>/g, ">"); | ||
var finalVal = this.val.trim().replace(/</g, "<").replace(/>/g, ">").replace(/&/g, '&'); | ||
@@ -219,2 +223,3 @@ if (options && options.trimmed && finalVal.length > 25) | ||
parser.oncdata = parser_cdata; | ||
parser.onerror = parser_error; | ||
} | ||
@@ -227,2 +232,3 @@ | ||
function parser_cdata() { delegates[0]._cdata.apply(delegates[0],arguments) } | ||
function parser_error() { delegates[0]._error.apply(delegates[0],arguments) } | ||
@@ -229,0 +235,0 @@ // a relatively standard extend method |
@@ -9,6 +9,6 @@ { | ||
}, | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"main": "./index", | ||
"dependencies": { | ||
"sax": "~0.6.1" | ||
"sax": "~1.1.1" | ||
}, | ||
@@ -15,0 +15,0 @@ "license": { |
@@ -20,7 +20,9 @@ | ||
var xmldoc = require('../lib/xmldoc'); | ||
```js | ||
var xmldoc = require('../lib/xmldoc'); | ||
var document = new xmldoc.XmlDocument("<some>xml</some>"); | ||
var document = new xmldoc.XmlDocument("<some>xml</some>"); | ||
... do things | ||
// do things | ||
``` | ||
@@ -35,3 +37,3 @@ ## Classes | ||
* `name` - the node name, like "tat" for `<tat>`. | ||
* `name` - the node name, like "tat" for `<tat>`. XML "namespaces" are ignored by the underlying [sax-js](https://github.com/isaacs/sax-js) parser, so you'll simply get "office:body" for `<office:body>`. | ||
* `attr` - an object dict containing attribute properties, like `bookNode.attr.title` for `<book title="...">`. | ||
@@ -49,2 +51,6 @@ * `val` - the string "value" of the node, if any, like "world" for `<hello>world</hello>`. | ||
It's important to note that `xmldoc` is designed for when you know exactly what you want from your XML file. For instance, it's great for parsing API responses with known structures, but it's not great at teasing things out of HTML documents from the web. | ||
If you need to do lots of searching through your XML document, I highly recommend trying a different library like [node-elementtree](https://github.com/racker/node-elementtree). | ||
### eachChild(func) | ||
@@ -70,13 +76,17 @@ | ||
<book> | ||
<author> | ||
<name isProper="true">George R. R. Martin</name> | ||
... | ||
</author> | ||
... | ||
</book> | ||
```xml | ||
<book> | ||
<author> | ||
<name isProper="true">George R. R. Martin</name> | ||
... | ||
</author> | ||
... | ||
</book> | ||
``` | ||
If you just want the `<name>` node and you have the `XmlElement` for the `<book>` node, you can say: | ||
var nameNode = bookNode.descendantWithPath("author.name"); // return <name> node | ||
```js | ||
var nameNode = bookNode.descendantWithPath("author.name"); // return <name> node | ||
``` | ||
@@ -87,7 +97,11 @@ ### valueWithPath(path) | ||
var authorName = bookNode.valueWithPath("author.name"); // return "George R. R. Martin" | ||
```js | ||
var authorName = bookNode.valueWithPath("author.name"); // return "George R. R. Martin" | ||
``` | ||
You can also use the `@` character to request the value of a particular _attribute_ instead: | ||
var authorIsProper = bookNode.valueWithPath("author.name@isProper"); // return "true" | ||
```js | ||
var authorIsProper = bookNode.valueWithPath("author.name@isProper"); // return "true" | ||
``` | ||
@@ -102,9 +116,13 @@ This is not [XPath][]! It's just a thing I made up, OK? | ||
xml.toString({compressed:true}) // strips indents and linebreaks | ||
xml.toString({trimmed:true}) // trims long strings for easier debugging | ||
```js | ||
xml.toString({compressed:true}) // strips indents and linebreaks | ||
xml.toString({trimmed:true}) // trims long strings for easier debugging | ||
``` | ||
Putting it all together: | ||
var xml = "<author><name>looooooong value</name></author>"; | ||
console.log("My document: \n" + new XmlDocument(xml).toString(trimmed:true)) | ||
```js | ||
var xml = "<author><name>looooooong value</name></author>"; | ||
console.log("My document: \n" + new XmlDocument(xml).toString(trimmed:true)) | ||
``` | ||
@@ -111,0 +129,0 @@ Prints: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17176
228
138
+ Addedsax@1.1.6(transitive)
- Removedsax@0.6.1(transitive)
Updatedsax@~1.1.1