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

xmlobject

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xmlobject - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

125

index.js

@@ -13,3 +13,3 @@ var sax = require("sax");

// Children
if(node.children) {
if(node.children.length > 0) {
result += '>\n';

@@ -32,17 +32,50 @@ node.children.forEach(function(c) {

constructor(name) {
constructor(name, parent) {
if(!name) throw new Error("node should have at least a name");
this.name = name;
this.parent = parent;
this.attributes = {};
this.namespaces = [];
this.children = [];
}
getAttribute(name) {
// #region Attributes
getAttribute(ns, name) {
if(name)
{
var prefix = this.getNamespacePrefix(ns);
name = prefix + ":" + name;
}
else name = ns;
return this.attributes[name];
}
setAttribute(name, value) {
setAttribute(ns, name, value) {
if(value)
{
var prefix = this.getNamespacePrefix(ns);
name = prefix + ":" + name;
}
else
{
value = name;
name = ns;
}
if(name.startsWith("xmlns:"))
{
var res = name.split(":");
this.addNamespace(res[1], value);
}
else if(name === "xmlns")
{
this.addNamespace("", value);
}
this.attributes[name] = value;
}
// #region Text
getTextNodes() {

@@ -67,2 +100,50 @@ return this.children.filter(function(c) { return typeof(c) === 'string'; });

// #region Namespaces
namespace() {
var res = this.name.split(":");
if(res.length == 2)return this.getNamespaceValue(res[0]);
else return this.getNamespaceValue("");
}
addNamespace(prefix, url) {
this.namespaces.push({ prefix: prefix, value: url });
this.attributes["xmlns:"+prefix] = url;
}
nameWithoutPrefix() {
var res = this.name.split(":");
if(res.length == 2) return res[1];
else return this.name;
}
getNamespacePrefix(value) {
var ns = this.namespaces.find(function(n) { return n.value === value;});
if(ns) return ns.prefix;
if(this.parent) return this.parent.getNamespacePrefix(value);
return null;
}
getNamespaceValue(prefix) {
var ns = this.namespaces.find(function(n) { return n.prefix === prefix;});
if(ns) return ns.value;
if(this.parent) return this.parent.getNamespaceValue(prefix);
return null;
}
// #region Children
createChild(ns,name) {
if(name != null)
{
var prefix = this.getNamespacePrefix(ns);
name = prefix + ":" + name;
}
else name = ns;
var child = new Node(name, this);
this.addChild(child);
return child;
}
addChild(c) {

@@ -72,10 +153,14 @@ this.children.push(c);

findChildren(name) {
return this.children.filter(function(c) { return c.name === name;});
findChildren(ns, name) {
if(name )return this.children.filter(function(c) { return name == c.nameWithoutPrefix() && c.namespace() === ns;});
return this.children.filter(function(c) { return ns == c.name });
}
firstChild(name) {
return this.children.find(function(c) { return c.name === name;});
firstChild(ns,name) {
if(name)return this.children.find(function(c) { return name == c.nameWithoutPrefix() && c.namespace() === ns;});
return this.children.find(function(c) { return ns == c.name });
}
// #region Serialization
static deserialize(s,cb) {

@@ -87,7 +172,9 @@ var stack = [];

parser.onopentagstart = function (node) {
current = new Node(node.name);
if(stack.length > 0)
{
stack[stack.length - 1].addChild(current);
if(stack.length > 0) {
var parent = stack[stack.length - 1];
current = parent.createChild(node.name);
}
else {
current = new Node(node.name);
}
stack.push(current);

@@ -113,7 +200,17 @@ };

asJSON() {
return JSON.stringify(this);
asObject() {
return {
name: this.name,
attributes: this.attributes,
namespaces: this.namespaces,
children: this.children.map(function(c) {
if (typeof c === 'string') return c;
return c.asObject();
})
}
}
asJSON() { return JSON.stringify(this.asObject()); }
}
module.exports = Node;

2

package.json
{
"name": "xmlobject",
"version": "0.5.0",
"version": "0.6.0",
"description": "Convert javascript objects to and from xml strings.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -18,6 +18,12 @@ # xmlobject

var s = '<a b="c" d="e"><f i="j">g<h /></f></a>';
var ns = "http://www.example.com/xml/test"
var defaultns = "http://www.example.com/xml/default"
var s = '<a xmlns="'+defaultns+'" xmlns:test="'+ns+'" b="c" d="e"><test:f test:i="j">g<h /></test:f></a>';
XML.deserialize(s, function(err,r) {
console.log(r.asJSON())
var f1 = r.firstChild(ns,"f");
console.log("i: " + f1.getAttribute(ns, "i"));
console.log("f: " + f1.getText());
});

@@ -30,15 +36,20 @@

a.setAttribute("d","e");
var f = new XML("f");
f.setAttribute("i","j");
a.addNamespace("test", ns)
var f = a.createChild(ns,"f");
f.setAttribute(ns,"i","j")
f.addChild("g");
f.addChild(new XML("h"))
a.addChild(f);
f.createChild("h");
a.serialize(function(err, r) {
console.log(r)
console.log(r);
});
```
## Warning
*xmlobject* is a perfect fit for small parsing scenario where all data could remain in memory but would not be perfectly optimized for parsing mega-octets of xml content where a stream/sax based approach would be better.
## Copyright and license
MIT © [Aloïs Deniel](http://aloisdeniel.github.io)
var XML = require('.');
var assert = require('assert');
var s = '<a xmlns:test="http://www.example.com/xml/test" b="c" d="e"><test:f test:i="j">g<h /></test:f></a>';
// Deserialize from xml
var a = new XML("a");
a.setAttribute("xmlns:test","http://www.example.com/xml/test");
a.setAttribute("b","c");
a.setAttribute("d","e");
var f = new XML("test:f");
f.setAttribute("test:i","j");
f.addChild("g");
f.addChild(new XML("h"))
a.addChild(f);
var ns = "http://www.example.com/xml/test"
var defaultns = "http://www.example.com/xml/default"
var s = '<a xmlns="'+defaultns+'" xmlns:test="'+ns+'" b="c" d="e"><test:f test:i="j">g<h /></test:f></a>';
XML.deserialize(s, function(err,r) {
console.log(err)
console.log(r.asJSON())
var f1 = r.firstChild("test:f");
console.log("i: " + f1.getAttribute("test:i"));
var f1 = r.firstChild(ns,"f");
console.log("i: " + f1.getAttribute(ns, "i"));
console.log("f: " + f1.getText());

@@ -27,5 +18,16 @@

// Serialize to xml
var a = new XML("a");
a.setAttribute("b","c");
a.setAttribute("d","e");
a.addNamespace("test", ns)
var f = a.createChild(ns,"f");
f.setAttribute(ns,"i","j")
f.addChild("g");
f.createChild("h");
a.serialize(function(err, r) {
console.log(err);
console.log(r);
});
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