1 Attachment(s)
[RESOLVED] Help with reading an XML-Type File
Hi,
I am trying to read in and eventually manipulate an 'XML-type' file. I have picked up a hobby called 'Geocaching', some people probably have heard of it, where you use gps coords to locate items placed by other people.
Well, you can download the gps coors within data files from the website,'Geocaching.com' and these files are in a XML format.
There are a lot of programs out there that people offer, some are better than others, but I am not happy with the results. So, I am trying to see how I can use these files myself and create my own program to use the data in a way that I want to use it.
I have tried to use Linq, XMLdocument, XMLreader and probably a couple of others over the past week. I have had marginal sucess with each of these methods, but nothing great and I owe that to my lack of knowledge.
I eventually hope to add the data to a Access db that I am using and manipulate the info from the db.
I am attaching a sample of one of the downloaded files and if someone would have the patience to help me work my way through it, I would be greatful.
Thanks,
Richard
p.s. The file typically ends with '.gpx', but since its an xml and thats what the system recognizes, I added the extension in order to upload the sample file.
Re: Help with reading an XML-Type File
ok.... so..... what's the question?
-tg
Re: Help with reading an XML-Type File
Quote:
Originally Posted by
techgnome
ok.... so..... what's the question?
-tg
Sorry about that.
The long and short of it, is I want to take the data from the 'xml-type' file and insert it into an Access database. Which from there I can manipulate the data for use in my program.
Now, whats the best way to get the data from the xml-type file to the db.
Thanks,
Re: Help with reading an XML-Type File
generally, for me, it's usually to load the data into an XMLDocument, select out the nodes I'm interested in, and insert them in to the database.
You could also see what happens if you try to import the XML document into Access (don't know if it can handle xml) ... are you looking for a one-time deal? Or something to do this on an ongoing basis?
-tg
Re: Help with reading an XML-Type File
Quote:
Originally Posted by
techgnome
generally, for me, it's usually to load the data into an XMLDocument, select out the nodes I'm interested in, and insert them in to the database.
You could also see what happens if you try to import the XML document into Access (don't know if it can handle xml) ... are you looking for a one-time deal? Or something to do this on an ongoing basis?
-tg
This is an ongoing basis. Basically what I want to do, is download these files as I find ones on the website for me to search for and import it into my program so that I can keep track on my laptop a bit better than I have been. Thats why I want to import this file into a access db, that way I can keep track of the ones I have already visited and the ones I havent and the ones that I keep having to go back on because I cant find it.
I first tried the xmldocument, but wasnt able to figure out how to select out the nodes and assign them to their respective variable before importing it into the db. I understand the import part into the db, its just getting nodes from the xmldocument.
Re: Help with reading an XML-Type File
Code:
Dim xml_doc as New XmlDocument
xml_doc.Load(filepath)
Dim root as XmlElement = xml_doc.DocumentElement
root would be the top-level node. Called "gpx" in your example file.
root.ChildNodes(0) would be the "name" node, etc. root.ChildNodes(0).innertext would return "Cache Listing Generated from Geocaching.com"
root.childnodes(8).Attributes(0).Value would return the minlat value, and so on.
Using that format, you can get the info you need. I'm guessing the Latitude, Longitude, name, etc.
Re: Help with reading an XML-Type File
Quote:
Originally Posted by
Spirited Machine
Code:
Dim xml_doc as New XmlDocument
xml_doc.Load(filepath)
Dim root as XmlElement = xml_doc.DocumentElement
root would be the top-level node. Called "gpx" in your example file.
root.ChildNodes(0) would be the "name" node, etc. root.ChildNodes(0).innertext would return "Cache Listing Generated from Geocaching.com"
root.childnodes(8).Attributes(0).Value would return the minlat value, and so on.
Using that format, you can get the info you need. I'm guessing the Latitude, Longitude, name, etc.
Is there a chart/page or something online that I could look at to get a better feel for how that works? I know that this is something that is taught in school, but I dont have the time or the money to go that direction.
So I am home schooled. (haha home schooled at 44 years old......)
Re: Help with reading an XML-Type File
MSDN .... that's going to be your best friend in all this... best way to search is to enter terms in the form {CLASSNAME}.[FUNCTION}..... so in this case your search terms should be xmldocument.documentelement ... and xmlnode.childnode you'll also want to probably look at xmlnode.selectnodes and xmlnode.selectsinglenode.
-tg
side note - I'm 100% self-taught too. I don't do well in classrooms. There's nothing wrong with it. I think you might be surprised at how many of us here fall into that self taught category.
Re: Help with reading an XML-Type File
So, if I am understanding you correctly, then
Code:
Dim C_Time = m_node.ChildNodes.Item(6).InnerText
would get me '2010-04-19T18:04:43.6811819Z' in the variable "C_Time".
and
Code:
Dim Lat = root.childnodes(8).Attributes(0).value
Code:
Dim Lon = root.childnodes(8).Attributes(1).value
would get me '42.1706' in the variable "Lat" and '-84.4142' in the variable "Lon".
How do I get to the next set of data thats contained within the <wpt .....> section and further in?
Re: Help with reading an XML-Type File
Code:
m_node.ChildNodes(9).Childnodes(x).InnerText
Re: Help with reading an XML-Type File
Quote:
Originally Posted by
techgnome
MSDN .... that's going to be your best friend in all this... best way to search is to enter terms in the form {CLASSNAME}.[FUNCTION}..... so in this case your search terms should be xmldocument.documentelement ... and xmlnode.childnode you'll also want to probably look at xmlnode.selectnodes and xmlnode.selectsinglenode.
-tg
side note - I'm 100% self-taught too. I don't do well in classrooms. There's nothing wrong with it. I think you might be surprised at how many of us here fall into that self taught category.
Where did you get most of your information to learn from? Books, online, etc... What books would you suggest? What websites would you suggest?
Re: Help with reading an XML-Type File
I read... A LOT! I learned to see the difference between books that are reference style - which get used over and over and over, vs tutorial style books - where you read once, do the examples and never read again. There are a few that are in between. MSDN (msdn.microsoft.com) ... like I said, get to know that, it should be your first stop when looking up something, Google second, here third. I don't normally like to recommend things because people learn in different ways... some like to have the code handed to them, others just need to be pointed in the right direction, others can take a sample code and then tailor it to their own needs.
To answer your question, what you could do is a For Each loop....
Code:
For Each mNode As XMLElement In Root.childnodes
Dim Lat = mNode.Attributes("lat").value
Dim Lon = mNode.Attributes("lon").value
'Do what you need to do with Lat/Lon here
Next
NOTE: XML is CASE SENSITIVE ... Lat and LAT and lat are NOT THE SAME THING. Also, I haven't looked at the XML, so I don't know what the proper attribute name is, so you may need to change the lat & lon.
-tg
Re: Help with reading an XML-Type File
I have a BS in computer science and all we programmed in was Java, C++ and C (except for the occasional fortran/cobal/rpg/etc.. in survey). I don't think many universities actually teach .NET.
Justin
Re: Help with reading an XML-Type File
Well, I think I have figured out how to use Xmldocument to get what I want done. It appears to work, but now I find myself with another issue.
I have logs inside these files that I download and the number of logs within the files varies. I have seen one with up to 20 different entries.
I first started to do this:
Code:
'Log 1 ------------------------------------------------------
dim log_id_1 = root.childnodes(9).childnodes(7).childnodes(12).childnodes(1).attributes(0).value
dim log_type_1 = root.childnodes(9).childnodes(7).childnodes(12).childnodes(1).childnodes(1).innertext
dim log_text_1 = root.childnodes(9).childnodes(7).childnodes(12).childnodes(1).childnodes(3).innertext
'------------------------------------------------------------
'Log 2-------------------------------------------------------
Then I realized that the amount of logs could vary. So though I would do a While loop, but I kept getting errors with this
Code:
while .......
i=i+1
'Log 1 ------------------------------------------------------
dim log_id_(i) = root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).attributes(0).value
dim log_type_(i) = root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(1).innertext
dim log_text_(i) = root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(3).innertext
'------------------------------------------------------------
'log 2 -------------------------------------------------------
I kept getting errors because I wasnt allowed to put a variable type in the name of a variable, ex: dim log_id(i) <--- the i is not allowed it stated.
So, the question I have is how to make the amount of times this would work be variable and have it adjust accordingly.
I hope I was clear enough with what I was saying.
Re: Help with reading an XML-Type File
I think I see what you're doing... Maybe you should use ArrayLists instead of trying to do that stuff.
Like so:
vb Code:
Dim log_id as new ArrayList
Dim log_type as new Arraylist
Dim log_text as new Arraylist
while .......
i=i+1
'Log 1 ------------------------------------------------------
dim log_id.Add(root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).attributes(0).value.ToString)
dim log_type.Add(root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(1).innertext.ToString)
dim log_text.Add(root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(3).innertext.ToString)
'------------------------------------------------------------
'log 2 -------------------------------------------------------
Oh, or better yet, make a class that actually keeps the values that are associated together:
VB Code:
Public Class XMLStuff
Private _Id as string
Private _type as string
Private _text as string
Public Sub New (byval Id as string,byval type as string,byval text as string)
_Id = Id
_type = type
_text = text
End Sub
' then set up your properties...
End Class
Then you could do:
vb Code:
Dim XMLStuffs as new ArrayList
while .......
i=i+1
'Log 1 ------------------------------------------------------
dim log_id as string = root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).attributes(0).value.ToString
dim log_type as string = (root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(1).innertext.ToString
dim log_text as string = root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(3).innertext.ToString
XMLStuffs.Add(new XMLStuff(log_id,log_type,log_text))
'------------------------------------------------------------
'log 2 -------------------------------------------------------
Justin
Re: Help with reading an XML-Type File
Quote:
Originally Posted by
Spirited Machine
Code:
m_node.ChildNodes(9).Childnodes(x).InnerText
How would I test to see if a node was there or not. In the files, sometimes there are things called travelbugs and they are listed in there like this.
Code:
</groundspeak:logs>
- <groundspeak:travelbugs>
- <groundspeak:travelbug id="805283" ref="TB19TQT">
<groundspeak:name>First To Find Geocoin</groundspeak:name>
</groundspeak:travelbug>
</groundspeak:travelbugs>
But, sometimes there are none of those travel bugs listed within that file. All thats there is a is this.
Code:
</groundspeak:logs>
<groundspeak:travelbugs />
So how do I test to for this situation.
Re: Help with reading an XML-Type File
God, I love doing this stuff......
When you figure it out and it all works, its like you mastered your mountain before you.
To get the program to recognize whether theres data within a node, like my travelbug one
Code:
While Not root.(childnodes(9).childnodes(7).childnodes(13).childnodes(1) Is Nothing
do program
End While
Works like a charm.