1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

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"> <script type="text/x-red" data-template-name="xml2js">
<div class="form-row"> <div class="form-row">
<label>Use Console</label> <label>Use Console</label>
<input type="checkbox" id="node-input-useEyes" placeholder="Name" style="display: inline-block; width: auto; vertical-align: top;"> <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> <label for="node-input-useEyes" style="width: 70%;">Debug output in console ?</label>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<div class="form-tips">Uses xml2js to process xml into javascript object.</div> <div class="form-tips">Uses xml2js to process xml into javascript object.</div>
</script> </script>
<script type="text/x-red" data-help-name="xml2js"> <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>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>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>
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('xml2js',{ RED.nodes.registerType('xml2js',{
category: 'advanced-function', category: 'advanced-function',
color:"#E6E0F8", color:"#E6E0F8",
defaults: { defaults: {
useEyes: {value:false}, useEyes: {value:false},
name: {value:""}, name: {value:""},
}, },
inputs:1, inputs:1,
outputs:1, outputs:1,
icon: "arrow-in.png", icon: "arrow-in.png",
label: function() { label: function() {
return this.name||"xml2js"; return this.name||"xml2js";
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
} }
}); });
</script> </script>

View File

@ -19,27 +19,28 @@ var util = require("util");
var parseString = require('xml2js').parseString; var parseString = require('xml2js').parseString;
var gotEyes = false; var gotEyes = false;
try { try {
var eyes = require("eyes"); var eyes = require("eyes");
gotEyes = true; gotEyes = true;
} catch(e) { } 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) { function Xml2jsNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.useEyes = n.useEyes; this.useEyes = n.useEyes;
var node = this; var node = this;
this.on("input", function(msg) {
this.on("input", function(msg) { parseString(msg.payload, function (err, result) {
parseString(msg.payload, function (err, result) { if (err) { node.error(err; }
msg.payload = result; else {
node.send(msg); msg.payload = result;
if (node.useEyes == true) { node.send(msg);
if (gotEyes == true) { eyes.inspect(msg); } if (node.useEyes == true) {
else { node.log(JSON.stringify(msg)); } if (gotEyes == true) { eyes.inspect(msg); }
} else { node.log(JSON.stringify(msg)); }
}); }
}); }
});
});
} }
RED.nodes.registerType("xml2js",Xml2jsNode); RED.nodes.registerType("xml2js",Xml2jsNode);