How to read xml external file

Hi all,
I do have a xml file and I wish to read it in a node-red flow to convert the data in payload flow.
does anybody has any sample about how to do it?
thanks

Hi @sanserg

Assuming the file is on the same computer you are running Node-RED on, then you will have the File node in the palette that can be used to read the contents of the file. You can then pass it through an XML node to parse it to a JavaScript object.

thanks @knolleary
I do not have much experience on node-red dev but will try ..

@knolleary or team.
asking some help. I created a flow with timestamp, file, xml and debug node. the issue I am having is that it reads only the first line of the xml file. Do you know if I am missing something?
I am sharing the export flow and the xml file if anybody can help.
Thanks

[{"id":"b0c63c38.8b2d","type":"file in","z":"33026990.71c786","name":"xml file","filename":"/home/pi/node-red/SpotData_small.xml","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":280,"y":380,"wires":[["b3bd08bf.50f5e8","f06b2752.341d58"]]},{"id":"b3bd08bf.50f5e8","type":"xml","z":"33026990.71c786","name":"","property":"payload","attr":"","chr":"","x":550,"y":440,"wires":[["f5dcaddf.c724f"]]},{"id":"f5dcaddf.c724f","type":"debug","z":"33026990.71c786","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":380,"wires":},{"id":"d03be1bc.99eec","type":"inject","z":"33026990.71c786","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":380,"wires":[["b0c63c38.8b2d"]]},{"id":"f06b2752.341d58","type":"debug","z":"33026990.71c786","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":560,"y":500,"wires":}]

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 2 LIVE 1207714554 0-3523746 0-3523746 EXTREME-TRACK 2019-05-30T21:29:16.000Z 1559251756 -19.97508 -43.57687 GOOD 1207714567 0-3524188 0-3524188 EXTREME-TRACK 2019-05-30T21:29:19.000Z 1559251759 -19.97505 -43.57648 GOOD 1 LIVE 1207714610 0-3524812 0-3524812 EXTREME-TRACK 2019-05-30T21:29:25.000Z 1559251765 -19.97504 -43.57657 GOOD 2 LIVE 1207714648 0-3523817 0-3523817 EXTREME-TRACK 2019-05-30T21:29:30.000Z 1559251770 -19.97501 -43.5766 GOOD 1207714661 0-3523889 0-3523889 EXTREME-TRACK 2019-05-30T21:29:32.000Z 1559251772 -19.97513 -43.57626 GOOD

Hi @sanserg - this post describes how to properly share code/flows in the forum so it gets formatted properly. Otherwise it may not be importable. How to share code or flow json

And you can see it has stripped off all the XML tags, so your XML example isn't readable.

thanks @knolleary to share and sorry as I do not have much experience to post here:

[{"id":"b0c63c38.8b2d","type":"file in","z":"33026990.71c786","name":"xml file","filename":"/home/pi/node-red/SpotData_small.xml","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":280,"y":380,"wires":[["b3bd08bf.50f5e8","f06b2752.341d58"]]},{"id":"b3bd08bf.50f5e8","type":"xml","z":"33026990.71c786","name":"","property":"payload","attr":"","chr":"","x":550,"y":440,"wires":[["f5dcaddf.c724f"]]},{"id":"f5dcaddf.c724f","type":"debug","z":"33026990.71c786","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":380,"wires":[]},{"id":"d03be1bc.99eec","type":"inject","z":"33026990.71c786","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":380,"wires":[["b0c63c38.8b2d"]]},{"id":"f06b2752.341d58","type":"debug","z":"33026990.71c786","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":560,"y":500,"wires":[]}]


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<messageList xmlns="http://v2.shared.globalstar.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://v2.shared.globalstar.com http://share.findmespot.com/shared/schema/spotXml-v2.xsd">
    <header>
        <totalCount>2</totalCount>
        <mode>LIVE</mode>
    </header>
    <message>
        <id>1207714554</id>
        <esn>0-3523746</esn>
        <esnName>0-3523746</esnName>
        <messageType>EXTREME-TRACK</messageType>
        <messageDetail></messageDetail>
        <timestamp>2019-05-30T21:29:16.000Z</timestamp>
        <timeInGMTSecond>1559251756</timeInGMTSecond>
        <latitude>-19.97508</latitude>
        <longitude>-43.57687</longitude>
        <batteryState>GOOD</batteryState>
    </message>
    <message>
        <id>1207714567</id>
        <esn>0-3524188</esn>
        <esnName>0-3524188</esnName>
        <messageType>EXTREME-TRACK</messageType>
        <messageDetail></messageDetail>
        <timestamp>2019-05-30T21:29:19.000Z</timestamp>
        <timeInGMTSecond>1559251759</timeInGMTSecond>
        <latitude>-19.97505</latitude>
        <longitude>-43.57648</longitude>
        <batteryState>GOOD</batteryState>
    </message>
</messageList>

<messageList xmlns="http://v2.shared.globalstar.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://v2.shared.globalstar.com http://share.findmespot.com/shared/schema/spotXml-v2.xsd">
    <header>
        <totalCount>1</totalCount>
        <mode>LIVE</mode>
    </header>
    <message>
        <id>1207714610</id>
        <esn>0-3524812</esn>
        <esnName>0-3524812</esnName>
        <messageType>EXTREME-TRACK</messageType>
        <messageDetail></messageDetail>
        <timestamp>2019-05-30T21:29:25.000Z</timestamp>
        <timeInGMTSecond>1559251765</timeInGMTSecond>
        <latitude>-19.97504</latitude>
        <longitude>-43.57657</longitude>
        <batteryState>GOOD</batteryState>
    </message>
</messageList>

<messageList xmlns="http://v2.shared.globalstar.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://v2.shared.globalstar.com http://share.findmespot.com/shared/schema/spotXml-v2.xsd">
    <header>
        <totalCount>2</totalCount>
        <mode>LIVE</mode>
    </header>
    <message>
        <id>1207714648</id>
        <esn>0-3523817</esn>
        <esnName>0-3523817</esnName>
        <messageType>EXTREME-TRACK</messageType>
        <messageDetail></messageDetail>
        <timestamp>2019-05-30T21:29:30.000Z</timestamp>
        <timeInGMTSecond>1559251770</timeInGMTSecond>
        <latitude>-19.97501</latitude>
        <longitude>-43.5766</longitude>
        <batteryState>GOOD</batteryState>
    </message>
    <message>
        <id>1207714661</id>
        <esn>0-3523889</esn>
        <esnName>0-3523889</esnName>
        <messageType>EXTREME-TRACK</messageType>
        <messageDetail></messageDetail>
        <timestamp>2019-05-30T21:29:32.000Z</timestamp>
        <timeInGMTSecond>1559251772</timeInGMTSecond>
        <latitude>-19.97513</latitude>
        <longitude>-43.57626</longitude>
        <batteryState>GOOD</batteryState>
    </message>
</messageList>

I'm not sure that XML is valid.

Pretty sure the whole XML needs to be inside a root element and not just a bunch of separate messageList items.

Try pasting it into one of the many online XML validation web sites.

If you have no control over how the XML is formatted or can't change it, then you may have to read the contents as a string and manipulate it into valid XML before passing through the XML node.

1 Like

So to verify, yes, your XML needs to be wrapped in a root element. It's currently not valid XML.

1 Like

You are right!
Just added a new root parent and it worket!
Thanks
Sandra

1 Like

I have read the URL XML file and I present all the tables so my question is about how can I save it in SQL.

This thread is about parsing XML and is 3 years old.

Please open a new topic and provide sufficient details so that others can help. (e.g. show what data you have, show your flow, explain what type of database and the table structure etc)

EDIT: I see you already have an open post on the forum. Please stick to that.