Results 1 to 17 of 17

Thread: [RESOLVED] Help with reading an XML-Type File

  1. #1

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    Resolved [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.
    Attached Files Attached Files
    Last edited by RichardKnox; Jun 22nd, 2010 at 11:29 AM. Reason: More info about what I want to do with the info from xml file
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Help with reading an XML-Type File

    ok.... so..... what's the question?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    Re: Help with reading an XML-Type File

    Quote Originally Posted by techgnome View Post
    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,
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    Re: Help with reading an XML-Type File

    Quote Originally Posted by techgnome View Post
    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.
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

  6. #6
    Addicted Member Spirited Machine's Avatar
    Join Date
    May 2009
    Posts
    215

    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.
    Last edited by Spirited Machine; Jun 22nd, 2010 at 03:07 PM.

  7. #7

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    Re: Help with reading an XML-Type File

    Quote Originally Posted by Spirited Machine View Post
    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......)
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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&#37; 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.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    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?
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

  10. #10
    Addicted Member Spirited Machine's Avatar
    Join Date
    May 2009
    Posts
    215

    Re: Help with reading an XML-Type File

    Code:
    m_node.ChildNodes(9).Childnodes(x).InnerText

  11. #11

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    Re: Help with reading an XML-Type File

    Quote Originally Posted by techgnome View Post
    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?
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  14. #14

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    Red face 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.
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

  15. #15
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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:
    1. Dim log_id as new ArrayList
    2. Dim log_type as new Arraylist
    3. Dim log_text as new Arraylist
    4.  
    5. while .......
    6. i=i+1
    7.  
    8. 'Log 1 ------------------------------------------------------
    9. dim log_id.Add(root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).attributes(0).value.ToString)
    10. dim log_type.Add(root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(1).innertext.ToString)
    11. dim log_text.Add(root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(3).innertext.ToString)
    12. '------------------------------------------------------------
    13.  
    14. 'log 2 -------------------------------------------------------

    Oh, or better yet, make a class that actually keeps the values that are associated together:

    VB Code:
    1. Public Class XMLStuff
    2.  
    3. Private _Id as string
    4. Private _type as string
    5. Private _text as string
    6.  
    7. Public Sub New (byval Id as string,byval type as string,byval text as string)
    8.  
    9. _Id = Id
    10. _type = type
    11. _text = text
    12.  
    13. End Sub
    14.  
    15. ' then set up your properties...
    16.  
    17. End Class

    Then you could do:


    vb Code:
    1. Dim XMLStuffs as new ArrayList
    2.  
    3. while .......
    4. i=i+1
    5.  
    6. 'Log 1 ------------------------------------------------------
    7. dim log_id as string = root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).attributes(0).value.ToString
    8. dim log_type as string = (root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(1).innertext.ToString
    9. dim log_text as string = root.childnodes(9).childnodes(7).childnodes(12).childnodes(i).childnodes(3).innertext.ToString
    10.  
    11. XMLStuffs.Add(new XMLStuff(log_id,log_type,log_text))
    12. '------------------------------------------------------------
    13.  
    14. 'log 2 -------------------------------------------------------

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  16. #16

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    Talking Re: Help with reading an XML-Type File

    Quote Originally Posted by Spirited Machine View Post
    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.
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

  17. #17

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    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.
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width