Trying to edit a xml file

Hello everyone, i'm a beginner with Node-RED and I would like to modify the value of all the "PORT" tags of a XML file that looks like this :

<SR><STATE><SYSERROR>TRUE</SYSERROR><PLXR>FALSE</PLXR><CHAMPWR>ON</CHAMPWR><MIX>ON</MIX><8100MIX>ON</8100MIX><PLXRMIX>ON</PLXRMIX><PLXRFAN>ON</PLXRFAN><CHAMBER><ATT>0</ATT><POS>CLOSED</POS><PORT>0</PORT></CHAMBER><V2>0</V2><V3>0</V3><V4>0</V4><GAS>0</GAS><INSTRUMENT>ACTIVE</INSTRUMENT><CUROB>1</CUROB><TOTALOB>12001</TOTALOB><OBCOMPLETE>42</OBCOMPLETE><NEXTOB>0</NEXTOB><PURGETYPE></PURGETYPE><PURGING>0</PURGING><IRGA>READY</IRGA><VIN>OK</VIN><GPS>FALSE</GPS></STATE></SR><CRC>3975194949</CRC>
<SR><DATA><TIME>20200303140128</TIME><CO2>650.84</CO2><CPRIME>656.68</CPRIME><CO2ABS>0.12116</CO2ABS><H2O>8.891</H2O><H2OABS>0.06417</H2OABS><BENCHPRESSURE>95.92</BENCHPRESSURE><BENCHTEMP>51.2</BENCHTEMP><CHAMBERTEMP>21.21</CHAMBERTEMP><BOARDTEMP>31.8</BOARDTEMP><VIN>12.12</VIN><RAWCO2>2808932</RAWCO2><RAWCO2REF>3433053</RAWCO2REF><RAWH2O>1674283</RAWH2O><RAWH2OREF>1784228</RAWH2OREF><FLUX>0.2</FLUX><RH>33.73</RH><V1>0.739</V1><V2>0.276</V2><V3>0.194</V3><V4>-0.068</V4><T1>21.62</T1><T2>-190.89</T2><T3>-190.89</T3><T4>183.62</T4><LATITUDE>0</LATITUDE><LONGITUDE>0</LONGITUDE><STATUS>0</STATUS><SPEED>0</SPEED><COURSE>0</COURSE></DATA></SR><CRC>2937005336</CRC>

So I've been inspired by my teacher who made a dashboard from the values of the "CO2", "H20", "bench pressure" and "bench temp" tags :

but when I try to do the same to change the value of from lines from 0 to1, it just seems like they are not in the msg.payload (we can't see STATE but only DATA in the debug window):

I would be really thankful if someone knows where is my mistake :smiley:

  • Put a debug after XML node (you will see a JS object representation of the XML)
  • Inspect the msg to find the path to the PORT property & click the "copy path button" that appears
  • Paste that path into a change node to set its value
  • Then convert the JS object back to XML by passing it through another XML node

First of all, thank you for your quick answer :slight_smile:

So I've tried to put the debug after the XML Node but it still seems like my object contains no "STATE", but I don't understand where's the problem.

I also need to say that I just Copied and past my teacher's inject node that has this JSON payload (that I don't understand)
image

An on that massively unnecessarily spaced out screenshot - did you fully expand the debug output?
The value you seek should be there.

BUT - that said - as you dont also have a debug BEFORE the XML node - how can you be 100% certain of the data going in actually has that STATE tag?

you dont understand JSON? or you dont understand why its there? or you dont understand what its doing later in the flow? To me it looks like an artefact of some past COPY+PASTE (i.e. it looks like its doing nothing)

If I am honest, you look like you could benefit from 30 minutes watching this playlist. It is really easy to digest and it will help you enormously in node-red.

After watching them, you'll fix this no problem.

I did and found no STATE tag, just the tags that are inside the tag.

When I put a debug before the XML node, it looks like the msg.payload contains the "STATE lines" of the XML file.

In fact I didn't understand what it has to do here, but has you said, i'll take a look at the playlist you send me, thank you very much.

look harder :wink:

Demo flow (with your data)

[{"id":"abe41aa7.69dad8","type":"inject","z":"e360ac5b.0ab79","name":"<SR>   <STATE>","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"<SR>   <STATE>","payload":"<SR>   <STATE>   \t<SYSERROR>TRUE</SYSERROR>     <PLXR>FALSE</PLXR>     <CHAMPWR>ON</CHAMPWR>     <MIX>ON</MIX>    <PLXRMIX>ON</PLXRMIX>     <PLXRFAN>ON</PLXRFAN>     <CHAMBER>       <ATT>0</ATT>       <POS>CLOSED</POS>       <PORT>0</PORT>       </CHAMBER>     <V2>0</V2>     <V3>0</V3>     <V4>0</V4>     <GAS>0</GAS>     <INSTRUMENT>ACTIVE</INSTRUMENT>     <CUROB>1</CUROB>     <TOTALOB>12001</TOTALOB><OBCOMPLETE>42</OBCOMPLETE>     <NEXTOB>0</NEXTOB><PURGETYPE></PURGETYPE><PURGING>0</PURGING><IRGA>READY</IRGA>     <VIN>OK</VIN><GPS>FALSE</GPS></STATE></SR>","payloadType":"str","x":600,"y":480,"wires":[["57fa7fff.84c0e","e4411999.c94c18"]]},{"id":"57fa7fff.84c0e","type":"xml","z":"e360ac5b.0ab79","name":"","property":"payload","attr":"","chr":"","x":770,"y":460,"wires":[["d734f8c4.7d0828","3d780b3.3d1b8f4"]]},{"id":"3212c29d.9a51ce","type":"inject","z":"e360ac5b.0ab79","name":"<SR><DATA>","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"<SR><DATA>","payload":"<SR><DATA><TIME>20200303140128</TIME><CO2>650.84</CO2><CPRIME>656.68</CPRIME><CO2ABS>0.12116</CO2ABS><H2O>8.891</H2O><H2OABS>0.06417</H2OABS><BENCHPRESSURE>95.92</BENCHPRESSURE><BENCHTEMP>51.2</BENCHTEMP><CHAMBERTEMP>21.21</CHAMBERTEMP><BOARDTEMP>31.8</BOARDTEMP><VIN>12.12</VIN><RAWCO2>2808932</RAWCO2><RAWCO2REF>3433053</RAWCO2REF><RAWH2O>1674283</RAWH2O><RAWH2OREF>1784228</RAWH2OREF><FLUX>0.2</FLUX><RH>33.73</RH><V1>0.739</V1><V2>0.276</V2><V3>0.194</V3><V4>-0.068</V4><T1>21.62</T1><T2>-190.89</T2><T3>-190.89</T3><T4>183.62</T4><LATITUDE>0</LATITUDE><LONGITUDE>0</LONGITUDE><STATUS>0</STATUS><SPEED>0</SPEED><COURSE>0</COURSE></DATA></SR>","payloadType":"str","x":590,"y":520,"wires":[["57fa7fff.84c0e","e4411999.c94c18"]]},{"id":"e4411999.c94c18","type":"debug","z":"e360ac5b.0ab79","name":"XML String In","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":800,"y":420,"wires":[]},{"id":"d734f8c4.7d0828","type":"debug","z":"e360ac5b.0ab79","name":"JS Object","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":920,"y":460,"wires":[]},{"id":"3d780b3.3d1b8f4","type":"change","z":"e360ac5b.0ab79","name":"","rules":[{"t":"set","p":"payload.SR.STATE[0].CHAMBER[0].PORT[0]","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":520,"wires":[["411ab0b4.0d2a5","6367173f.964a58"]]},{"id":"411ab0b4.0d2a5","type":"debug","z":"e360ac5b.0ab79","name":"JS Object mod","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1060,"y":560,"wires":[]},{"id":"6367173f.964a58","type":"xml","z":"e360ac5b.0ab79","name":"","property":"payload","attr":"","chr":"","x":1110,"y":460,"wires":[["6f1d449b.52c71c"]]},{"id":"6f1d449b.52c71c","type":"debug","z":"e360ac5b.0ab79","name":"XML String Out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1140,"y":400,"wires":[]}]

Definitely watch those videos - most of what I am saying here are demonstrated in them. :+1:

It seems like it works by injecting and separately and thank you for giving me the demo(it will sure help me), but when i work with my file which contains an alternation of and it definitely only finds the DATA tag.


STATE should be at the same place as DATA, but even by

I'm currently watching the videos, hope they'll give me the solution !
thank you again

That's probably because the data is not valid XML - the data you send above is like 4 XML parts...

Try pasting it into an online XML validator

1 Like

Oh, the file comes from meteorological station datas and I didn't even checked if it was good.
I finally can see the STATE tag in my javaObject, thank you very much :smiley:

Hope I'll be more efficient to create a modified file from the first one haha

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.