Page 1 of 5 123 ... LastLast
Results 1 to 10 of 41

Thread: Parsing XML to get one value

  1. #1
    Join Date
    Jul 2007
    Location
    The Bavarian Alps
    Beans
    129
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Parsing XML to get one value

    This is probably so easy that no tutorial anywhere deals with it. Sorry but I just can not find where to start.

    I have an XML file as follows
    </gpx>
    <trk>
    <name>070626KappellerAlp</name>
    <desc></desc>
    <trkseg>
    <trkpt lat="47.62788" lon="10.49729">
    <ele>876.38</ele>
    <time>2007-06-26T14:48:07.000Z</time>
    </trkpt>
    <trkpt lat="47.62777" lon="10.49757">
    <ele>876.38</ele>
    <time>2007-06-26T14:48:10.000Z</time>
    </trkpt>
    </trkseg>
    </trk>
    </gpx>

    The only data I am interested in at the moment is between the <ele> tags. Later I will need others but not yet.

    I want to open the file, find the first <ele>, do something with it and then go on to the next <ele> and do it again.
    I want to cary on like this until there are no more <ele> left at which point I am finished.

    It sounds so easy but for a newbie Python programmer it seems nearly impossible. I have read all about SAX and that seems to be what I need but I am totally confused.

    Could one of you experts please spare a few minutes to point me in the correct direction.

    Thank you very much in advance.

    For any one interested - this is part of a mountain bike track in GPX format and I am aiming at a profiler that calculates climb and descent based on a freely given threshhold.

  2. #2
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: Parsing XML to get one value

    Code:
    from xml.etree import ElementTree
    
    if __name__ == "__main__":
    	tree = ElementTree.parse("test.xml")
    	root = tree.getroot()
    	iter = root.getiterator("ele")
    	for i in iter:
    		print i.text
    But I think this will be more useful to you:

    Code:
    from xml.etree import ElementTree
    
    if __name__ == "__main__":
    	tree = ElementTree.parse("test.xml")
    	root = tree.getroot()
    	trackSegments = root.getiterator("trkseg")
    	for trackSegment in trackSegments:
    		for trackPoint in trackSegment:
    			print trackPoint.attrib['lat']
    			print trackPoint.attrib['lon']
    			print trackPoint.find('ele').text
    			print trackPoint.find('time').text

  3. #3
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Parsing XML to get one value

    Quote Originally Posted by Quikee View Post
    Code:
    from xml.etree import ElementTree
    
    if __name__ == "__main__":
    	tree = ElementTree.parse("test.xml")
    	root = tree.getroot()
    	iter = root.getiterator("ele")
    	for i in iter:
    		print i.text
    But I think this will be more useful to you:

    Code:
    from xml.etree import ElementTree
    
    if __name__ == "__main__":
    	tree = ElementTree.parse("test.xml")
    	root = tree.getroot()
    	trackSegments = root.getiterator("trkseg")
    	for trackSegment in trackSegments:
    		for trackPoint in trackSegment:
    			print trackPoint.attrib['lat']
    			print trackPoint.attrib['lon']
    			print trackPoint.find('ele').text
    			print trackPoint.find('time').text
    I'd suggest using regex ...

    Perl version:
    Code:
    open $file, "test.xml" or die $!;
    @array = map { $_ =~ /<ele>(.*?)<\/ele>/ } <$file>;
    for $i (@array) {
      print $array[$i] . "\n";
    }
    close $file;
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  4. #4
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Parsing XML to get one value

    Quote Originally Posted by slavik View Post
    I'd suggest using regex ...
    And accept invalid XML without even blinking?
    Not even tinfoil can save us now...

  5. #5
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: Parsing XML to get one value

    Quote Originally Posted by slavik View Post
    I'd suggest using regex ...

    Perl version:
    Code:
    open $file, "test.xml" or die $!;
    @array = map { $_ =~ /<ele>(.*?)<\/ele>/ } <$file>;
    for $i (@array) {
      print $array[$i] . "\n";
    }
    close $file;
    Sorry, but I don't see a single point (except in speed maybe) why this would be better.

  6. #6
    Join Date
    Jul 2007
    Location
    The Bavarian Alps
    Beans
    129
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: Parsing XML to get one value

    Hello Quikee!

    First thank you. Your code was short and understandable. Exactly what some one like me needs. And it was Python as I asked for

    I used your second suggestion on a test xml file (the same as I copied in to the question and "it worked"

    Thanks!

    I then tried with my "real file" and I get no output.
    I have copied the file below. Before I get in to an orgy of debugging - can you tell me which part is stopping the output.

    Once again thanks. I love Python and Ubuntu - not because I always understand them but because some kind soul will always help me

    <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
    <gpx
    version="1.1"
    creator="Touratech QV 4.0.87 Standard - http://www.ttqv.com"
    xmlnssi="http://www.w3.org/2001/XMLSchema-instance" xmlns:topografix="http://www.topografix.com/GPX/Private/TopoGrafix/0/1"
    xmlns="http://www.topografix.com/GPX/1/1"
    xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
    <metadata>
    <time>2007-09-02T17:30:53Z</time>
    <bounds minlat="47.4481272697449" minlon="10.4949045181274" maxlat="47.6289081573486" maxlon="11.0962772369385"/>
    </metadata>
    <trk>
    <name>070902Garmisch</name>
    <cmt>ActiveLog_2007-09-02_001
    Download Date: 02.09.2007 19:20:26</cmt>
    <trkseg>
    <trkpt lat="47.6289081573486" lon="10.4949045181274">
    <ele>867</ele>
    <time>2007-09-02T06:04:35Z</time>
    </trkpt>
    </trkseg>
    </trk>
    <extensions>
    </extensions>
    </gpx>

  7. #7
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Parsing XML to get one value

    Quote Originally Posted by aks44 View Post
    And accept invalid XML without even blinking?
    In this situation, who cares if you have valid xml if you only care about certain tags?
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  8. #8
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Parsing XML to get one value

    Quote Originally Posted by slavik View Post
    In this situation, who cares if you have valid xml if you only care about certain tags?
    Because if the XML isn't valid then the contents of the tag is probably incorrect as well?
    Not even tinfoil can save us now...

  9. #9
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Parsing XML to get one value

    Using the DOM is much better than using regex. Now, the contents of the elements can be processed with regex, but this is not some arbitrary string, it an XML file.

  10. #10
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Parsing XML to get one value

    Quote Originally Posted by aks44 View Post
    Because if the XML isn't valid then the contents of the tag is probably incorrect as well?
    or if you haven't downloaded the entire file?

    Jabber conversations are continuous XML streams (which causes a problem when one user disconnects)
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

Page 1 of 5 123 ... LastLast

Bookmarks

Posting Permissions

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