Trying to parse a XML file [SOLVED]

Hey there, I got the following XML file, and I’m trying to figure out how to parse this file;

<?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>3</totalCount>
        <mode>LIVE</mode>
    </header>
    <message>
        <id>759402364</id>
        <esn>0-3100073</esn>
        <esnName>CAMINHAO VOLVO</esnName>
        <messageType>STOP</messageType>
        <messageDetail></messageDetail>
        <timestamp>2017-06-05T19:39:17.000Z</timestamp>
        <timeInGMTSecond>1496691557</timeInGMTSecond>
        <latitude>-16.70042</latitude>
        <longitude>-49.32733</longitude>
        <batteryState>GOOD</batteryState>
    </message>
    <message>
        <id>759402364</id>
        <esn>0-3100073</esn>
        <esnName>CAMINHAO VOLVO</esnName>
        <messageType>STOP</messageType>
        <messageDetail></messageDetail>
        <timestamp>2017-06-05T19:34:17.000Z</timestamp>
        <timeInGMTSecond>1496691257</timeInGMTSecond>
        <latitude>-16.70063</latitude>
        <longitude>-49.32733</longitude>
        <batteryState>GOOD</batteryState>
    </message>
    <message>
        <id>759402392</id>
        <esn>0-3107912</esn>
        <esnName>ONIBUS NAIARA</esnName>
        <messageType>NEWMOVEMENT</messageType>
        <messageDetail></messageDetail>
        <timestamp>2017-06-05T19:44:25.000Z</timestamp>
        <timeInGMTSecond>1496691865</timeInGMTSecond>
        <latitude>-16.64136</latitude>
        <longitude>-49.30286</longitude>
        <batteryState>GOOD</batteryState>
    </message>
</messageList>

I need to extract every and get the child nodes to put these data inside the database, every “message” tag should be a record I mean… Could anyone help me?
Tnx.

The general method to follow in this sort of situation is:

  1. Figure out what you have (the XML)
  2. Figure out what you need (rows in table)
  3. Figure out how to get from what you have to what you need.

The XML you provided handles 1.

For 2. you need to know the table name, schema, and database type (e.g., SQLite).

Without a complete answer for 2., I can’t give a reasonable solution for 3. However, the start of the solution is to xml.Unmarshal the XML into a variable with a type like:

type MessageList struct {
	Header struct {
		TotalCount int    `xml:"totalCount"`
		Mode       string `xml:"mode"`
	}
	Message []struct {
		Id              string    `xml:"id"`
		Esn             string    `xml:"esn"`
		EsnName         string    `xml:"esnName"`
		MessageType     string    `xml:"messageType"`
		MessageDetail   string    `xml:"messageDetail"`
		TimeStamp       time.Time `xml:"timestamp"`
		TimeInGMTSecond uint64    `xml:"timeInGMTSecond"`
		Latitude        float64   `xml:"latitude"`
		Longitude       float64   `xml:"longitude"`
		BatteryState    string    `xml:"batteryState"`
	} `xml:"message"`
}

The types I used may need to change depending on more complete answers to 1. and 2…

After you have successfully unmarshaled the XML, then you can loop through MessageList.Message and use db.Exec with a properly crafted insert statement to put the data in the database.

I noticed the first two messages are the same. Is this what actually happens in the data, or is it an artifact of creating sample data for this forum?

Hey buddy, thank you for your reply. No it’s not the same data, it’s the same device but different data, as you can see, there’s a little difference in datetime and lat/lng. Thanks for your help, Imma try it on here.

I saw now that there are 2 messages with the same ID, it must be a bug on the tracker…

EDIT: The repeated id is not the message id, but the devide ID, I saw not in the other logs…

1 Like

That’s my code right now, it compiles and executes without any error, but shows no data, XML has 3 “” and it shows 3 lines, but no data. What am I doing wrong?

package main

import (
	"encoding/xml"
	"fmt"
)

var xmlstr = `<?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>3</totalCount>
        <mode>LIVE</mode>
    </header>
    <message>
        <id>759402364</id>
        <esn>0-3100073</esn>
        <esnName>CAMINHAO VOLVO</esnName>
        <messageType>STOP</messageType>
        <messageDetail></messageDetail>
        <timestamp>2017-06-05T19:39:17.000Z</timestamp>
        <timeInGMTSecond>1496691557</timeInGMTSecond>
        <latitude>-16.70042</latitude>
        <longitude>-49.32733</longitude>
        <batteryState>GOOD</batteryState>
    </message>
    <message>
        <id>759402364</id>
        <esn>0-3100073</esn>
        <esnName>CAMINHAO VOLVO</esnName>
        <messageType>STOP</messageType>
        <messageDetail></messageDetail>
        <timestamp>2017-06-05T19:34:17.000Z</timestamp>
        <timeInGMTSecond>1496691257</timeInGMTSecond>
        <latitude>-16.70063</latitude>
        <longitude>-49.32733</longitude>
        <batteryState>GOOD</batteryState>
    </message>
    <message>
        <id>759402392</id>
        <esn>0-3107912</esn>
        <esnName>ONIBUS NAIARA</esnName>
        <messageType>NEWMOVEMENT</messageType>
        <messageDetail></messageDetail>
        <timestamp>2017-06-05T19:44:25.000Z</timestamp>
        <timeInGMTSecond>1496691865</timeInGMTSecond>
        <latitude>-16.64136</latitude>
        <longitude>-49.30286</longitude>
        <batteryState>GOOD</batteryState>
    </message>
</messageList>`

type Message struct {
	id        string `xml:"id"`
	esn       string `xml:"esn"`
	tipomsg   string `xml:"messageType"`
	datahora  string `xml:"timeInGMTSecond"`
	latitude  string `xml:"latitude"`
	longitude string `xml:"longitude"`
}

type Query struct {
	MessageList []Message `xml:"message"`
}

func main() {
	b := []byte(xmlstr)

	var q Query
	xml.Unmarshal(b, &q)

	for _, msg := range q.MessageList {
		fmt.Printf("%s - %s\n", msg.esn, msg.datahora)
	}
}

Tnx.

The fields in your Message struct need to be exported (i.e., start with capital letters) so encoding/xml can set them.

1 Like

Friends, I got this topic solved. Sorry I forgot to edit it. Thank you all for your help.

1 Like

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