Question

I am trying to extract the content from a gpx file. The problem is when I used getChildren("wpt") to get the content of wpt tag, I got nothing returned. And when I used getChildren() method, I got and several returned. And when I removed all the contents in the only leave it as , everything works fine.

The content of this file:

<?xml version="1.0" encoding="UTF-8"?>
    <gpx  version="1.0" creator="GPSBabel- 
     http://www.gpsbabel.org"   
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://www.topografix.com/GPX/1/0"   
     xsi:schemaLocation="http://www.topografix.com/GPX/1/0
     http://www.topografix.com/GPX/1/0/gpx.xsd">
            <time>2010-11-07T06:21:28Z</time> 
            <bounds minlat="40.516437500" minlon="-79.759539000"
             maxlat="44.943992000" maxlon="-72.186828500"/>
             <wpt lat="43.449895700" lon="-79.759539000">   
                  <name>Pharmacy</name>   
                  <cmt>Pharmacy</cmt>   
                  <desc>Pharmacy</desc> 
             </wpt> 
             <wpt lat="43.650977000" lon="-79.758495300">   
               <name>Pharmacy:Walk-In Clinic</name>  
               <cmt>Pharmacy:Walk-In Clinic</cmt>   
               <desc>Pharmacy:Walk-In Clinic</desc> 
            </wpt> 
            <wpt lat="43.583929100" lon="-79.758268700">   
               <name>Hospital:Meadowvale Professional Center</name>   
               <cmt>Hospital:Meadowvale Professional Center</cmt>   
               <desc>Hospital:Meadowvale Professional Center</desc>
           </wpt>
           </gpx>

Below is my codes to extract the content:

import org.jdom.*; 
import org.jdom.input.SAXBuilder; 
import java.sql.*;
import java.util.*;

public class ReadXml 
{
     public  Connection conn = null;
     public  Statement stmt = null ;

     public void readXml()
     {

         try
         {
            Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
            String url ="jdbc:mysql://localhost/test?user=root&password=admin";//
                   conn = DriverManager.getConnection(url); 
                   stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
         }
         catch(Exception sqlexception)
         {
            System.out.println("connection error !");
         }

        try
        { 
          SAXBuilder sb = new SAXBuilder();


          Document doc = sb.build("new_york_Government_and_Public_Services.gpx");
          Element root = doc.getRootElement();
              String name = "" ,lat = "", lon = "";
          Element elms = null;
              List list1 = root.getChildren("wpt");
          for(int i=0; i< list1.size(); i++)
          { 
              elms = (Element)list1.get(i);        
              lat = elms.getAttributeValue("lat");
              lon = elms.getAttributeValue("lon");
              name = elms.getChildText("name");
              String sql = "insert into poi_test(name,lat,lon)values               ('"+name+"','"+lat+"','"+lon+"')";
              stmt.executeUpdate(sql);

          }//for

          stmt.close(); 
          conn.close(); 
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

     }


     public static void main(String[] args)
     {
          ReadXml rx = new ReadXml();
                  rx.readXml();
     }
}
Was it helpful?

Solution

getChildren(String) JavaDoc:

This returns a List of all the child elements nested directly (one level deep) within this element with the given local name and belonging to no namespace, returned as Element objects. If this target element has no nested elements with the given name outside a namespace, an empty List is returned. The returned list is "live" in document order and changes to it affect the element's actual contents.

As your wpt tag belongs to http://www.topografix.com/GPX/1/0 namespace, the getChildren method behaves correctly when returning no children. Instead, you should use getChildren(String,Namespace):

Namespace gpx = Namespace.getNamespace("http://www.topografix.com/GPX/1/0");
//[...]
List list1 = root.getChildren("wpt", gpx);

OTHER TIPS

I believe you need to specify the element namespace in this instance:

//yada yada
Namespace rootNamespace = root.getNamespace();
List wptElements = root.getChildren("wpt", rootNamespace);
for (Iterator wptIt = wptElements.iterator(); wptIt.hasNext(); ) {
  Element wpt = (Element)wptIt.next();
  //yada yada
}

Please also note that its recommended to iterate through the list via an Iterator instead of by index, according to the JDOM javadocs:

Sequential traversal through the List is best done with a Iterator since the underlying implement of List.size() may not be the most efficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top