Java从xml文件中获取多个值

时间:2016-06-13 12:56:13

标签: java xml

我有一个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>

我需要获取值“coordinates”并将每个值从十进制转换为小时/分钟。

我已经让类转换了值:

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文件中获取输入。

我读了一些帖子但是,可能是我的错,我仍然没有得到它。

谁能解释我怎么做?

如果可能,请举例说明。

感谢。

1 个答案:

答案 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();
  }

}