Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xmldoc

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xmldoc - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

14

lib/xmldoc.js

@@ -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, "&lt;").replace(/>/g, "&gt;");
var finalVal = this.val.trim().replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/&/g, '&amp;');

@@ -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:

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc