Daniele_r81 Daniele_r81 - 5 months ago 7
Java Question

Java get multiple value from xml file

i have a xml file as below:

<?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 need to get the value "coordinates" and trasnform each value from decimal to dreegree/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 dont know if is it possible get the input from that xml file.
i read some post but, probably my fault, i still dont get it.
can anyone explain me how to do it ? if possible with some examples.
thanks

Answer

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

}
Comments