Parse Data from XML

Hi,

I am still learning node-red, I was able to complete one part of the project recently thanks to all of your help. I now have another part of the project I am stuck on. I have a XML file that I needed to parse data from. I will attach the XML file to the question. I was wondering if there was a way I can use a particular node that would help me parse data from the XML file.

Specifically I needed the TenderType and Amount. Any help would be appreciated. Thank you in advance.

incometest.txt (1.8 KB)

Try the xml node that will convert from xml to a javascript object or the other way.

Use xml and then link it to a debug node

Was just trying that node, how would I access the particular value from the JS object? maybe from a function?

you could use a function or a change node.

But use a debug node to take a look at the object and look at the “working with messages” page in the docs to see how useful the debug panel can be.

That did help it convert it into a JavaScript Object. I tried to put in a function node in between and maybe access some of the values in the object. How would you suggest I do that? Syntax wise in the function node.

Thank you

Hi @KevinPamu - as Mark mentioned, the https://nodered.org/docs/user-guide/messages page in the docs tells you how you can use the Debug sidebar to identify the path to any element in the message - it can copy that path straight to your clipboard.

Once you have that you could do something like:

msg.payload = msg.<paste-the-path-from-debug-sidebar-here>;
return msg;

Or similarly using the Change node.

Thank you for the reply @knolleary I tried doing that and receive this message, "TypeError: Cannot read property 'Transaction' of undefined". Will keep trying to work with it. However, I will receive more than one message like that and I was wondering if there would be a way I can have a set syntax to get the values for every xml file.

Thank you for the solution @knolleary @ukmoose . I have a follow up question, do you think there is a way I can find out the length of the TenderSummary array from the XML file?

LMGTFY: https://www.w3schools.com/jsref/jsref_length_array.asp

2 Likes

Hello! I have a similar problem when i try to extract data from a specific object from a room scheduling server.
"msg.payload.ArrayOfScreenOccasion.ScreenOccasion[0]"
What I'm trying to do is that "ObjectName[0]" contains the name of the room which i will use to the filter the data "msg.payload.ArrayOfScreenOccasion.ScreenOccasion[0]" and send the data to a screen mounted outside the room. Inside the "ScreenOccasion[0]" contains the following:

  • ObjectName[0] (Name of the room)
  • StartDate[0] (format: 2019-03-06T09:00:00+01:00)
  • EndDate[0] (format:2019-03-06T16:30:00+01:00)
  • ExtraInfo[0].ScreenOccasionExtraInfo[0].TextValue[0] (Contains the booking description)
  • ExtraInfo[0].ScreenOccasionExtraInfo[1].TextValue[0] (Contains the customer name)
    "msg.payload.ArrayOfScreenOccasion.ScreenOccasion[1]" is the next booking.
    all the bookings appears unsorted
    How do I extract the data and publish it to the right room?

So you have an array of bookings as it says "msg.payload.ArrayOfScreenOccasion"

You could either use JSONata to filter the data or use an old fashioned function node and iterate through the array

OK. Do you have an example?
The thing is that i have to publish the data to an another server which controls the panels outside the rooms via TCP/IP.

No but there’s an exerciser on the jsonata website where you can learn all about it

Ok. Do you have a link to the sonata website? :slight_smile:

i’m sure if you google jsonata you’ll find it

Ok. Thanks! I saw your post saying "sonata" before you edit it.