XML options through msg.options

214 views
Skip to first unread message

Eelco Folkertsma

unread,
Apr 16, 2017, 1:47:42 PM4/16/17
to Node-RED
Hi, 
The XML node can handle lots of options through "You can also pass in a msg.options object to override all the multitude of parameters."

Can some-one show me a coding example to render pretty XML? I have no clue what to write in Function node that sets msg.options before XML node

<node that generates JS object> -- <Function to set msg.options> -- <XML> --> pretty XML ;-))

Julian Knight

unread,
Apr 16, 2017, 2:12:17 PM4/16/17
to Node-RED
Where are you trying to render to? A web page? Dashboard?

Eelco Folkertsma

unread,
Apr 16, 2017, 2:21:22 PM4/16/17
to Node-RED
I am mapping XML from one xsd into another by converting into/out of JSON (so XML-node on way and on way out). The resulting XML is posted to a broker using MQTT, and then I display the XML on CloudAMQP dasboard (get message).

Other people work in Java and generate nicely looking XML. I am only able to generate very long strings and am looking for "pretty xml" capability

Julian Knight

unread,
Apr 16, 2017, 3:56:16 PM4/16/17
to Node-RED
Well the XML node will convert between json and XML both ways but other than that, Node-RED is more oriented around JSON so there are no specific nodes for pretty-printing XML. I would normally dump to a file and then open in a text editor that has a pretty-print plugin for XML. Something like Notepad++, Sublime Text or Microsoft Visual Studio Code.

Eelco Folkertsma

unread,
Apr 16, 2017, 4:02:23 PM4/16/17
to Node-RED
Actually, the XML node has option to pretty print. I just do not understand instructions to get it working.
One has to pass an object in msg.options that contains settings per xml2js instructions. But how do I code such an object?

Eelco Folkertsma

unread,
Apr 17, 2017, 1:52:50 PM4/17/17
to Node-RED
Solved!

You can influence behavior of XML node by setting msg.options. For pretty print https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/README.md#options refers tho the builder class

Options for the Builder class

These options are specified by new Builder({optionName: value}). Possible options are:

  • rootName (default root or the root key name): root element name to be used in case explicitRoot is false or to override the root element name.
  • renderOpts (default { 'pretty': true, 'indent': ' ', 'newline': '\n' }): Rendering options for xmlbuilder-js.
    • pretty: prettify generated XML
etc etc

It should be translated in (I did use a Function-node)
// pretty print
msg.options={renderOpts: { 'pretty': true, 'indent': '  ', 'newline': '\n' }};

Be aware you need to have rather recent version of node-red. Some bug was fixed end 2016 (which is how I discovered the use)

Julian Knight

unread,
Apr 17, 2017, 3:00:23 PM4/17/17
to Node-RED
Cool, thanks for sharing, could be useful. Glad you got it sorted.
Reply all
Reply to author
Forward
0 new messages