Java从xml文件中获取多个值

时间:2022-11-27 17:09:58

I have a xml file as below:

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

And I need to get the value "coordinates" and transform each value from decimal to hours/minutes.

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

I already made the class to convert the value:

我已经让类转换了值:

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);
    } 

But I don't know if is it possible to get the input from that xml file.

但我不知道是否有可能从该xml文件中获取输入。

I read some posts but, probably my fault, I still don't get it.

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

Can anyone explain me how to do it?

任何人都可以解释我该怎么做?

If possible with some examples.

如果可能,举一些例子。

Thanks.

1 个解决方案

#1


1  

Could you please try following solution:

您可以尝试以下解决方案:

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

}

#1


1  

Could you please try following solution:

您可以尝试以下解决方案:

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

}