2016-06-13 22 views
-1

得到多个值,我有如下一个XML文件:从Java的XML文件

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://www.opengis.net/kml/2.2"> 
    <Document><name>My document</name> 
     <description>Content</description> 
     <Style id="Lump"> 
      <LineStyle><color>CD0000FF</color><width>2</width></LineStyle> 
      <PolyStyle><color>9AFF0000</color></PolyStyle> 
     </Style> 
     <Style id="Path"> 
      <LineStyle><color>FF0000FF</color><width>3</width></LineStyle> 
     </Style> 
     <Style id="markerstyle"> 
      <IconStyle><Icon><href> 
      http://maps.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png 
      </href></Icon></IconStyle> 
     </Style> 
     <Placemark><name>NAME</name> 
      <description>YES</description> 
      <styleUrl>#Path</styleUrl> 
      <LineString> 
       <tessellate>1</tessellate> 
       <altitudeMode>clampToGround</altitudeMode> 
       <coordinates> 
        12.656250,41.454049,0.0 
        12.676849,41.454049,0.0 
        12.693329,41.448903,0.0 
        12.746887,41.418015,0.0 
        12.936401,41.370625,0.0 
        12.966614,41.351041,0.0 
        12.992706,41.323201,0.0 
        13.010559,41.298444,0.0 
        13.024292,41.293285,0.0 
        13.024292,41.287094,0.0 
       </coordinates> 
      </LineString> 
     </Placemark> 
    </Document> 
</kml> 

,并需要获得值“坐标”,并从十进制trasnform每个值dreegree /分钟。 我已经取得的类值转换:

public ConvertCooDecToLatLong(Double latDec, Double lonDec) { 

     latH = (double) latDec.intValue(); 
     latM = (double)(new Double((latDec - latH) * 60.0).intValue()); 
     latS = (new Double(((latDec - latH) * 60.0) - latM) * 60); 

     lonH = (double) latDec.intValue(); 
     lonM = (double)(new Double((latDec - lonH) * 60.0).intValue()); 
     lonS = (new Double(((latDec - lonH) * 60.0) - lonM) * 60); 

     if (latDec > 0) { 
      dirLat = "N"; 
     } else { 
      dirLat = "S"; 
     } 

     if (lonDec > 0) { 
      dirLong = "E"; 
     } else { 
      dirLong = "W"; 
     } 

     coord = new LLEle("TEST", latH, latM, latS, dirLat, lonH, lonM, lonS, dirLong); 
    } 

,但我不知道是否是有可能得到的是XML文件的输入。 我读了一些帖子,但可能是我的错,我仍然没有得到它。 任何人都可以解释我如何做到这一点?如果可能的话用一些例子。 感谢

+0

你已经知道如何从XML得到其他典型值? – Zircon

+1

https://docs.oracle.com/javase/tutorial/jaxp/ – Fildor

+0

虐待尝试,谢谢 –

回答

1

能否请您尝试以下解决方案:

import java.io.File; 
import java.io.IOException; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.xml.sax.SAXException; 

public class Test { 

public static void main(String[] args) throws Exception { 
    Test test = new Test(); 
    String str = test.getCoordinatesContent("D:/test.xml"); 
    System.out.println(str); 
} 

public String getCoordinatesContent(String filePath)throws ParserConfigurationException, SAXException, IOException { 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
    Document doc = dBuilder.parse(new File(filePath)); 
    doc.getDocumentElement().normalize(); 
    return doc.getElementsByTagName("coordinates").item(0).getTextContent().trim(); 
    } 

} 
+0

它的工作原理,谢谢 –