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

Let XML node options be set

let msg.options to set a lot more options if required
This commit is contained in:
Dave Conway-Jones 2015-09-26 13:46:35 +01:00
parent caa83ac830
commit fa42fbdab8
3 changed files with 22 additions and 11 deletions

View File

@ -23,10 +23,10 @@
</div> </div>
<div id="advanced-options"> <div id="advanced-options">
<div class="form-row"> <div class="form-row">
<i class="fa fa-key"></i> <span data-i18n="xml.label.represent"></span> <input type="text" id="node-input-attr" style="width:20px !important"> <i class="fa fa-key"></i> <span data-i18n="xml.label.represent"></span> <input type="text" id="node-input-attr" style="width:20px !important" placeholder="$">
</div> </div>
<div class="form-row"> <div class="form-row">
<i class="fa fa-key"></i> <span data-i18n="xml.label.prefix"></span> <input type="text" id="node-input-chr" style="width:20px !important"> <i class="fa fa-key"></i> <span data-i18n="xml.label.prefix"></span> <input type="text" id="node-input-chr" style="width:20px !important" placeholder="_">
</div> </div>
<div class="form-tips"><span data-i18n="xml.tip"></span></div> <div class="form-tips"><span data-i18n="xml.tip"></span></div>
</div> </div>
@ -36,7 +36,10 @@
<p>A function that parses the <b>msg.payload</b> to convert xml to/from a javascript object. Places the result in the payload.</p> <p>A function that parses the <b>msg.payload</b> to convert xml to/from a javascript object. Places the result in the payload.</p>
<p>If the input is a string it tries to parse it as XML and creates a javascript object.</p> <p>If the input is a string it tries to parse it as XML and creates a javascript object.</p>
<p>If the input is a javascript object it tries to build an XML string.</p> <p>If the input is a javascript object it tries to build an XML string.</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>You can also pass in a <b>msg.options</b> object to overide all the multitude of parameters. 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>If set, options in the edit dialogue override those passed in on the msg.options object.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -45,8 +48,8 @@
color:"#DEBD5C", color:"#DEBD5C",
defaults: { defaults: {
name: {value:""}, name: {value:""},
attr: {value:'$',required:true}, attr: {value:""},
chr: {value:'_',required:true} chr: {value:""}
}, },
inputs:1, inputs:1,
outputs:1, outputs:1,

View File

@ -22,17 +22,25 @@ module.exports = function(RED) {
function XMLNode(n) { function XMLNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.attrkey = n.attr || '$'; this.attrkey = n.attr;
this.charkey = n.chr || '_'; this.charkey = n.chr;
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) { if (msg.hasOwnProperty("payload")) {
if (typeof msg.payload == "object") { if (typeof msg.payload === "object") {
msg.payload = builder.buildObject(msg.payload); var options = {};
if (msg.hasOwnProperty("options") && typeof msg.options === "object") { options = msg.options; }
options.async = false;
msg.payload = builder.buildObject(msg.payload, options);
node.send(msg); node.send(msg);
} }
else if (typeof msg.payload == "string") { else if (typeof msg.payload == "string") {
parseString(msg.payload, {strict:true,async:true,attrkey:node.attrkey,charkey:node.charkey}, function (err, result) { var options = {};
if (msg.hasOwnProperty("options") && typeof msg.options === "object") { options = msg.options; }
options.async = true;
options.attrkey = node.attrkey || options.attrkey || '$';
options.charkey = node.charkey || options.charkey || '_';
parseString(msg.payload, options, function (err, result) {
if (err) { node.error(err, msg); } if (err) { node.error(err, msg); }
else { else {
msg.payload = result; msg.payload = result;

View File

@ -36,7 +36,7 @@
"mustache": "2.1.2", "mustache": "2.1.2",
"cron":"1.0.9", "cron":"1.0.9",
"raw-body":"2.1.2", "raw-body":"2.1.2",
"xml2js":"0.4.9", "xml2js":"0.4.12",
"sentiment":"0.2.3", "sentiment":"0.2.3",
"follow-redirects":"0.0.6", "follow-redirects":"0.0.6",
"cors":"2.7.1", "cors":"2.7.1",