Added error trap to xml2js node.

This commit is contained in:
Dave C-J 2013-10-22 20:25:14 +01:00
parent 612dd4dc7f
commit 3b60e1a0e3
2 changed files with 48 additions and 47 deletions

View File

@ -15,39 +15,39 @@
-->
<script type="text/x-red" data-template-name="xml2js">
<div class="form-row">
<label>Use Console</label>
<input type="checkbox" id="node-input-useEyes" placeholder="Name" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-useEyes" style="width: 70%;">Debug output in console ?</label>
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips">Uses xml2js to process xml into javascript object.</div>
<div class="form-row">
<label>Use Console</label>
<input type="checkbox" id="node-input-useEyes" placeholder="Name" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-useEyes" style="width: 70%;">Debug output in console ?</label>
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips">Uses xml2js to process xml into javascript object.</div>
</script>
<script type="text/x-red" data-help-name="xml2js">
<p>A function that parses the <b>msg.payload</b> using the xml2js library. Places the result in the payload.</p>
<p>See <a href="https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/README.md" target="_new">the xml2js docs <i>here</i></a> for more information.</p>
<p>A function that parses the <b>msg.payload</b> using the xml2js library. Places the result in the payload.</p>
<p>See <a href="https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/README.md" target="_new">the xml2js docs <i>here</i></a> for more information.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('xml2js',{
category: 'advanced-function',
color:"#E6E0F8",
defaults: {
useEyes: {value:false},
name: {value:""},
},
inputs:1,
outputs:1,
icon: "arrow-in.png",
label: function() {
return this.name||"xml2js";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
RED.nodes.registerType('xml2js',{
category: 'advanced-function',
color:"#E6E0F8",
defaults: {
useEyes: {value:false},
name: {value:""},
},
inputs:1,
outputs:1,
icon: "arrow-in.png",
label: function() {
return this.name||"xml2js";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -19,27 +19,28 @@ var util = require("util");
var parseString = require('xml2js').parseString;
var gotEyes = false;
try {
var eyes = require("eyes");
gotEyes = true;
var eyes = require("eyes");
gotEyes = true;
} catch(e) {
util.log("[73-parsexml.js] Warning: Module 'eyes' not installed");
util.log("[73-parsexml.js] Note: Module 'eyes' not installed. (not needed, but useful)");
}
function Xml2jsNode(n) {
RED.nodes.createNode(this,n);
this.useEyes = n.useEyes;
var node = this;
this.on("input", function(msg) {
parseString(msg.payload, function (err, result) {
msg.payload = result;
node.send(msg);
if (node.useEyes == true) {
if (gotEyes == true) { eyes.inspect(msg); }
else { node.log(JSON.stringify(msg)); }
}
});
});
RED.nodes.createNode(this,n);
this.useEyes = n.useEyes;
var node = this;
this.on("input", function(msg) {
parseString(msg.payload, function (err, result) {
if (err) { node.error(err; }
else {
msg.payload = result;
node.send(msg);
if (node.useEyes == true) {
if (gotEyes == true) { eyes.inspect(msg); }
else { node.log(JSON.stringify(msg)); }
}
}
});
});
}
RED.nodes.registerType("xml2js",Xml2jsNode);