Agora caso queira apenas alguns campos pode criar uma classe manualmente que corresponde ao xml e usar xstream pra deserializar o xml. Que é bem fácil tb.
em ultimo caso, pode pegar o xml e usar algo como expressão regular pra pegar o valor (não faria assim ehehehe)
e mandei ele imprimir a “Uf” :
“if (element.getQualifiedName().equals(“uf”))”
ele imprimiu corretamente a consulta, então não sei o que tem errado com o meu metodo
public static void main(String[] args) throws Exception{
String expressao = "//lat/text()";
String xml = "seuArquivoXml.xml";
XPath xPath = XPathFactory.newInstance().newXPath();
InputStream streamCatalogo = ExecutorXPath.class.getResourceAsStream(xml);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamSource source = new StreamSource(streamCatalogo);
DOMResult result = new DOMResult();
transformer.transform(source, result);
Node document = result.getNode();
NodeList nodeList = (NodeList) xPath.evaluate(expressao, document, XPathConstants.NODESET);
for(int i = 0 ; i < nodeList.getLength() ; i++){
Node node = nodeList.item(i);
System.out.println("Lat " + (i + 1) + ": " + node.getNodeValue());
}
}
}[/code]Aqui o resultado foi:Lat 1: -28.6331481
Lat 2: -28.6344931
Lat 3: -28.6317951
Lat 4: -28.6331771
Lat 5: -28.6331111
Lat 6: -28.6329291
Lat 7: -28.6342966
Lat 8: -28.6315986
Lat 9: -28.6331111
Lat 10: -28.6327841
public static void main(String[] args) throws Exception{
String expressao = "//lat/text()";
String xml = "seuArquivoXml.xml";
XPath xPath = XPathFactory.newInstance().newXPath();
InputStream streamCatalogo = ExecutorXPath.class.getResourceAsStream(xml);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamSource source = new StreamSource(streamCatalogo);
DOMResult result = new DOMResult();
transformer.transform(source, result);
Node document = result.getNode();
NodeList nodeList = (NodeList) xPath.evaluate(expressao, document, XPathConstants.NODESET);
for(int i = 0 ; i < nodeList.getLength() ; i++){
Node node = nodeList.item(i);
System.out.println("Lat " + (i + 1) + ": " + node.getNodeValue());
}
}
}[/code]Aqui o resultado foi:Lat 1: -28.6331481
Lat 2: -28.6344931
Lat 3: -28.6317951
Lat 4: -28.6331771
Lat 5: -28.6331111
Lat 6: -28.6329291
Lat 7: -28.6342966
Lat 8: -28.6315986
Lat 9: -28.6331111
Lat 10: -28.6327841[/quote]
OLa!!! Assim deu certo as como faço para buscar além da expressão “lat” a espressão “lng”
Bom, como você não definiu qual quer, até que fica fácil… se quer todos e e sempre vem em pares, é só utilizar a seguinte expressão XPath:String expressao = "//lat/..";