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

Add "advanced" options to XML parsing node

Allows setting of attrkey and charkey
Push to close #348
This commit is contained in:
Dave C-J 2014-12-15 17:05:18 +00:00
parent 1261bf97ea
commit bf8d549cf7
2 changed files with 34 additions and 2 deletions

View File

@ -17,8 +17,36 @@
<script type="text/x-red" data-template-name="xml"> <script type="text/x-red" data-template-name="xml">
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name" style="width:280px !important">
</div> </div>
<div class="form-row" id="advanced">
</div>
<div id="advanced-options">
<div class="form-row">
<i class="fa fa-key"></i> Represent XML tag attributes as a property named <input type="text" id="node-input-attr" style="width:20px !important">
</div>
<div class="form-row">
<i class="fa fa-key"></i> Prefix to access character content <input type="text" id="node-input-chr" style="width:20px !important">
</div>
<div class="form-tips">There is no simple way to convert XML attributes to JSON
so the approach taken here is to add a property, named $ by default, to the JSON structure.</div>
</div>
<script> {
var showadvanced = showadvanced || true;
var showall = function() {
showadvanced = !showadvanced;
if (showadvanced) {
$("#advanced-options").show();
$("#advanced").html('<label for="node-advanced" style="width:200px !important"><i class="fa fa-minus-square"></i> Advanced options</label>');
}
else {
$("#advanced-options").hide();
$("#advanced").html('<label for="node-advanced" style="width:200px !important"><i class="fa fa-plus-square"></i> Advanced options ...</label>');
}
};
showall();
$("#advanced").click( function() { showall(); });
} </script>
</script> </script>
<script type="text/x-red" data-help-name="xml"> <script type="text/x-red" data-help-name="xml">
@ -33,6 +61,8 @@
category: 'function', category: 'function',
color:"#DEBD5C", color:"#DEBD5C",
defaults: { defaults: {
attr: {value:'$',required:true},
chr: {value:'_',required:true},
name: {value:""} name: {value:""}
}, },
inputs:1, inputs:1,

View File

@ -22,6 +22,8 @@ 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.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")) {
@ -30,7 +32,7 @@ module.exports = function(RED) {
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}, function (err, result) { parseString(msg.payload, {strict:true,async:true,attrkey:node.attrkey,charkey:node.charkey}, function (err, result) {
if (err) { node.error(err); } if (err) { node.error(err); }
else { else {
msg.payload = result; msg.payload = result;