Prezados, estou tentando ler e obter os dados do arquivo do google earth, um xml de extensão .kml. Acontece que no .kml tem a seguinte situação:
<Placemark>
<Style>
<LineStyle>
<color>ff800000</color>
<width>5</width>
</LineStyle>
</Style>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>clampedToGround</altitudeMode>
<coordinates>
-38.52211291350152, -3.73997690255904, 0
-38.52226037814308, -3.74028716385334, 0
-38.52211291350152, -3.73997690255904, 0
</coordinates>
</LineString>
</Placemark>
<Placemark>
<description>endereço se quizer</description>
<name>cliente</name>
<Style> <IconStyle> <Icon> <href>root://icons/palette-4.png</href> <x>224</x> <y>96</y> <w>32</w> <h>32</h> </Icon> </IconStyle> </Style>
<Point>
<coordinates> -38.52211291350152, -3.73997690255904, 0</coordinates>
</Point>
</Placemark>
e usando o código que coloco mais a frente consigo o seguinte retorno(um List):
{Style= ff800000 5 , LineString= 1 1 clampedToGround -38.53381795467538, -3.73808026330819 , 0 -38.53389815333927, -3.73750714957512, 0 -38.53363441473085, -3.73759151243668, 0 -38.53300592318240, -3.73782964450525, 0 -38.53273697222071, -3.73793298406046, 0 -38.53178439316733, -3.73821149480332, 0 - 38.53113178217966, -3.73841870168225, 0 -38.53063932841026, -3.73860575934948, 0 -38.53063207788845, -3.73858224919858, 0 -38.52984455978773, -3.73885839205332, 0 -38.52903619404832, -3.73912471727858, 0 -38.52832922103589 , -3.73931548563222, 0 -38.52829755412934, -3.73935914739553, 0 -38.52760609504772, -3.73957718714098, 0 -38.52676654221278, -3.73984462713957, 0 -38.52640650116504, -3.73999641421698, 0 -38.52607860298271, - 3.74012482742984, 0 -38.52601912595244, -3.74008848574268, 0 -38.52493966669991, -3.74048784326727, 0 -38.52464390007905, -3.73989931055092, 0 -38.52404749700698, -3.73980886781742, 0 -38.52292976842390, -3.73961598489568 , 0 -38.52225862831646, -3.73949241902577, 0 -38.52225372018881, -3.73957338497666, 0 -38.52211291350152, -3.73997690255904, 0 },
{description=endereço se quizer, Style= root://icons/palette-4.png 224 96 32 32 , name=cliente,
Point= -38.53381795467538, -3.73808026330819, 0 },
ou seja, na primeira parte perco o acesso a <coordinates> e a outros dados interessantes.
Usando o dom e utilizando a seguinte classe para ler o xml:
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class XmlHelper {
public static List parseList(String xml, String path, String element) throws Exception {
String pathVet[] = path.substring(1).split("/");
SAXBuilder sb = new SAXBuilder();
Document document = sb.build(new StringReader(xml));
Element root = document.getRootElement();
/* Percorre o vetor pulando o elemento root */
for (int i = 1; i < pathVet.length; i++) {
root = root.getChild(pathVet[i]);
}
/* Cria a nova lista.*/
ArrayList result = new ArrayList(0);
/* Percorre a lista de elementos.*/
if (root != null) {
List elements = root.getChildren();
for (Iterator iter = elements.iterator(); iter.hasNext();) {
Element node = (Element) iter.next();
if (node.getName().equals(element)) {
List children = node.getContent();
HashMap map = new HashMap(children.size());
for (Iterator iterator = children.iterator(); iterator.hasNext();) {
Element child = (Element) iterator.next();
map.put(child.getName(), child.getValue());
}
result.add(map);
}
}
result.trimToSize();
}
return result;
}
}
a seguir a classe que utilizo para teste:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
public class lerXML {
public static void main (String args[]) throws Exception {
retornarDados();
}
public static List retornarDados() throws Exception{
FileReader reader = new FileReader("C:/Documents and Settings/Emanoel/Desktop/teste.kml");
BufferedReader leitor = new BufferedReader(reader);
StringBuffer dados = new StringBuffer();
while (leitor.ready()) {
dados.append(leitor.readLine());
}
String xml = dados.toString();
List dadosKml = XmlHelper.parseList(xml, "/Document", "Placemark");
System.out.println(dadosKml);
return null;
}
}
alguém sabe uma maneira de ler todos os elementos de um elemento pai, pois no exemplo acima nao tenho como acessar tag por tag.