XML value of all data

Hello!

I have an XML file.

How can I filter this value of all data?

payload.HRC_STOCK.PRODUCT [0]

https://www.hrcdistribution.com/xml/xml_feed_stock.php?c=8407

what bit are you stuck on

  • getting the XML from the web URL?
  • converting the XML to JS object?
  • getting the first element payload.HRC_STOCK.PRODUCT[0]?

If you want to access payload.HRC_STOCK.PRODUCT[0] in a function then remember to add msg. e.g. msg.payload.HRC_STOCK.PRODUCT[0]

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

Hello!

Many thanks for the answer.

I would like to have all the data from the file in an object.

But if I enter the following in the function, I only get one record back.

msg.payload = msg.payload.HRC_STOCK.PRODUCT [0]
return msg;

I can do that that everyone comes?

It should look like this with my CSV file
Bildschirmfoto 2021-07-26 um 22.18.33

Well, thats because you are only taking the first element.

If you don't do that, you will have all data in msg.payload.HRC_STOCK.PRODUCT where you can access the first object in [0] and the 2nd in [1] etc


If you want the ditch the HRC_STOCK.PRODUCT properties then simply assign that to the msg.payload...

msg.payload = msg.payload.HRC_STOCK.PRODUCT;
return msg;

Thanks for the reply

But there has to be a way to take all the data.

I can't make a command for every line. That's 7000 lines.

Is it possible to convert it to a CSV file?

Yes, but why? what are you doing with the data? Must you convert to CSV? CSV is so 1990! What is the ultimate goal for this data?

If you must...
image

image

[{"id":"a7fd9025.796f9","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":474,"y":1168,"wires":[["1793ba5a.ba2bc6"]]},{"id":"1793ba5a.ba2bc6","type":"http request","z":"1bd2b382.4dd4f4","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://www.hrcdistribution.com/xml/xml_feed_stock.php?c=8407","tls":"","persist":false,"proxy":"","authType":"","x":646,"y":1168,"wires":[["49fde8c4.ea29b8"]]},{"id":"49fde8c4.ea29b8","type":"xml","z":"1bd2b382.4dd4f4","name":"","property":"payload","attr":"","chr":"","x":832,"y":1168,"wires":[["cab07671.087e38"]]},{"id":"cab07671.087e38","type":"function","z":"1bd2b382.4dd4f4","name":"","func":"var data = msg.payload.HRC_STOCK.PRODUCT;\n\nmsg.payload = data.map(e => {\n    return { \n        \"ID\": Number(e.ID[0]),\n        \"REF\": e.REF[0],\n        \"STOCK\": Number(e.STOCK[0]),\n    }\n});\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":1168,"wires":[["bfc215a.db2aee8","8bb8fd56.a2ad5"]]},{"id":"bfc215a.db2aee8","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1142,"y":1168,"wires":[]},{"id":"8bb8fd56.a2ad5","type":"csv","z":"1bd2b382.4dd4f4","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":1024,"y":1232,"wires":[["ab242884.5e4f08"]]},{"id":"ab242884.5e4f08","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1174,"y":1232,"wires":[]}]

Hello!

the goal is to add up all payload.HRC_STOCK.PRODUCT [0] .STOCK [0] values.

Then CSV is not what you want...

[{"id":"a7fd9025.796f9","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":492,"y":1168,"wires":[["1793ba5a.ba2bc6"]]},{"id":"1793ba5a.ba2bc6","type":"http request","z":"1bd2b382.4dd4f4","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://www.hrcdistribution.com/xml/xml_feed_stock.php?c=8407","tls":"","persist":false,"proxy":"","authType":"","x":662,"y":1168,"wires":[["49fde8c4.ea29b8"]]},{"id":"49fde8c4.ea29b8","type":"xml","z":"1bd2b382.4dd4f4","name":"","property":"payload","attr":"","chr":"","x":818,"y":1168,"wires":[["cab07671.087e38"]]},{"id":"cab07671.087e38","type":"function","z":"1bd2b382.4dd4f4","name":"Sum of STOCK","func":"msg.payload = msg.payload.HRC_STOCK.PRODUCT.reduce((a,b) => {\n    return a + Number(b.STOCK[0]);\n}, 0)\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":1168,"wires":[["bfc215a.db2aee8"]]},{"id":"bfc215a.db2aee8","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1174,"y":1168,"wires":[]}]

Great that's exactly what I wanted. Thanks alot.

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