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.1.2 to 0.1.3

15

examples/example.js

@@ -7,7 +7,7 @@

// Demonstrate parsing an in-memory XML string
var xmlString = '<suggestions><book title="Twilight"/><book title="Twister"/></suggestions>'
var xmlString = '<suggestions><book title="Twilight"/><book title="Twister"/></suggestions>';
var suggestions = new XmlDocument(xmlString);
// Demonstrate how toString() will pretty-print an abbreviated version of the XML for debugging
// Demonstrate how toString() will pretty-print the XML for debugging
console.log("Parsed: \n%s", suggestions);

@@ -22,3 +22,3 @@

var fs = require('fs'),
path = require('path')
path = require('path');

@@ -34,5 +34,11 @@ fs.readFile(path.join(__dirname, "test.xml"), 'utf8', function (err,data) {

// Demonstrate toString() with an option to abbreviate long strings and compress the output
console.log("Parsed: \n%s", results.toString({trimmed:true, compressed:true}));
// Pull out the <books> node
var books = results.childNamed("books");
// Demonstrate firstChild/lastChild
console.log("First book has ISBN '%s', last book has ISBN '%s'", books.firstChild.attr.isbn, books.lastChild.attr.isbn);
// Print out the ISBNs

@@ -50,3 +56,3 @@ books.eachChild(function (book) {

// Search for a particular book
twilight = books.childWithAttribute("isbn","478-2-23-765712-2");
var twilight = books.childWithAttribute("isbn","478-2-23-765712-2");

@@ -56,2 +62,3 @@ // Result is a single XmlElement instance for <book>

return null;
});
(function () {
// global on the server, window in the browser
var sax, root = this;
var sax;
if (typeof module !== 'undefined' && module.exports) {
// We're being used in a Node-like environment
sax = require('sax');
root = module.exports;
}
else {
sax = root.sax;
// assume it's attached to the Window object in a browser
sax = this.sax;
if (!sax) // no sax for you!

@@ -26,2 +27,4 @@ throw new Error("Expected sax to be defined. Make sure you're including sax.js before this file.");

this.children = [];
this.firstChild = null;
this.lastChild = null;
}

@@ -43,15 +46,15 @@

delegates.unshift(child);
}
};
XmlElement.prototype._closetag = function() {
delegates.shift();
}
};
XmlElement.prototype._text = function(text) {
if (text) this.val += text;
}
};
XmlElement.prototype._cdata = function(cdata) {
if (cdata) this.val += cdata;
}
};

@@ -63,3 +66,3 @@ // Useful functions

if (iterator.call(context, this.children[i], i, this.children) === false) return;
}
};

@@ -71,3 +74,4 @@ XmlElement.prototype.childNamed = function(name) {

}
}
return null;
};

@@ -82,3 +86,3 @@ XmlElement.prototype.childrenNamed = function(name) {

return matches;
}
};

@@ -91,3 +95,4 @@ XmlElement.prototype.childWithAttribute = function(name,value) {

}
}
return null;
};

@@ -105,3 +110,3 @@ XmlElement.prototype.descendantWithPath = function(path) {

return descendant;
}
};

@@ -113,37 +118,40 @@ XmlElement.prototype.valueWithPath = function(path) {

return components.length > 1 ? descendant.attr[components[1]] : descendant.val;
}
else
return null;
};
// String formatting (for debugging)
XmlElement.prototype.toString = function() {
return this.toStringWithIndent("");
}
XmlElement.prototype.toString = function(options) {
return this.toStringWithIndent("", options);
};
XmlElement.prototype.toStringWithIndent = function(indent) {
var s = "";
s += indent + "<" + this.name;
XmlElement.prototype.toStringWithIndent = function(indent, options) {
var s = indent + "<" + this.name;
var linebreak = options && options.compressed ? "" : "\n";
for (var name in this.attr)
s += " " + name + '="' + this.attr[name] + '"';
if (Object.prototype.hasOwnProperty.call(this.attr, name))
s += " " + name + '="' + this.attr[name] + '"';
var trimVal = this.val.trim();
var finalVal = this.val.trim();
if (trimVal.length > 25)
trimVal = trimVal.substring(0,25).trim() + "…";
if (options && options.trimmed && finalVal.length > 25)
finalVal = finalVal.substring(0,25).trim() + "…";
if (this.children.length) {
s += ">\n";
s += ">" + linebreak;
var childIndent = indent + (options && options.compressed ? "" : " ");
var childIndent = indent + " ";
if (trimVal.length)
s += childIndent + trimVal + "\n";
if (finalVal.length)
s += childIndent + finalVal + linebreak;
for (var i=0, l=this.children.length; i<l; i++)
s += this.children[i].toStringWithIndent(childIndent) + "\n";
s += this.children[i].toStringWithIndent(childIndent, options) + linebreak;
s += indent + "</" + this.name + ">";
}
else if (trimVal.length) {
s += ">" + trimVal + "</" + this.name +">";
else if (finalVal.length) {
s += ">" + finalVal + "</" + this.name +">";
}

@@ -153,3 +161,3 @@ else s += "/>";

return s;
}
};

@@ -165,5 +173,5 @@ /*

if (!xml)
throw new Error("No XML to parse!")
throw new Error("No XML to parse!");
var parser = sax.parser(true) // strict
var parser = sax.parser(true); // strict
addParserEvents(parser);

@@ -189,3 +197,3 @@

XmlElement.prototype._opentag.apply(this,arguments);
}
};

@@ -219,4 +227,8 @@ // file-scoped global stack of delegates

root.XmlDocument = XmlDocument;
// Are we being used in a Node-like environment?
if (typeof module !== 'undefined' && module.exports)
module.exports.XmlDocument = XmlDocument;
else
this.XmlDocument = XmlDocument;
})()
})();

@@ -9,6 +9,6 @@ {

},
"version": "0.1.2",
"version": "0.1.3",
"main": "./index",
"dependencies": {
"sax": "0.4.2"
"sax": "~0.6.1"
},

@@ -15,0 +15,0 @@ "license": {

@@ -38,2 +38,3 @@

* `children` - an array of `XmlElement` children of the node.
* `firstChild`, `lastChild` - pretty much what it sounds like; null if no children

@@ -90,8 +91,15 @@ Each member defaults to a sensible "empty" value like `{}` for `attr`, `[]` for `children`, and `""` for `val`.

### toString()
### toString([options])
This is just an override of the standard JavaScript method, it will give you the pretty-printed string representation of your XML document or element. Note that this is for debugging only! It will truncate any long node values.
This is just an override of the standard JavaScript method, it will give you a string representation of your XML document or element. Note that this is for debugging only! It is not guaranteed to always output valid XML.
The default implementation of `toString()`, that is, the one you get when you just `console.log("Doc: " + myDoc)` will pretty-print the XML with linebreaks and indents. You can pass a couple options to control the output:
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))
console.log("My document: \n" + new XmlDocument(xml).toString(trimmed:true))

@@ -98,0 +106,0 @@ Prints:

Sorry, the diff of this file is not supported yet

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