leogazio
(Leonardo Gazio)
June 5, 2017, 9:00pm
1
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:
Figure out what you have (the XML)
Figure out what you need (rows in table)
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?
leogazio
(Leonardo Gazio)
June 5, 2017, 9:41pm
4
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.
leogazio
(Leonardo Gazio)
June 5, 2017, 9:43pm
5
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
leogazio
(Leonardo Gazio)
June 5, 2017, 10:57pm
6
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
leogazio
(Leonardo Gazio)
June 17, 2017, 9:02pm
8
Friends, I got this topic solved. Sorry I forgot to edit it. Thank you all for your help.
1 Like
system
(system)
Closed
September 15, 2017, 9:06pm
9
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.