Hello, il faut passer par un objet Document, comme ceci :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 URL google = new URL("http://www.monurl.com/monFichier.xml"); URLConnection googleConnection = google.openConnection(); InputSource dis; dis = new InputSource(googleConnection.getInputStream()); Document document = null; try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.parse(dis); } catch (ParserConfigurationException e) { e.printStackTrace(); } XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList nodes = (NodeList)xPath.evaluate("//variable1", document, XPathConstants.NODESET); String variable1 = (nodes.item(0).getTextContent().trim()); nodes = (NodeList)xPath.evaluate("//variable2", document, XPathConstants.NODESET); String variable2 = (nodes.item(0).getTextContent().trim()); nodes = (NodeList)xPath.evaluate("//variable3", document, XPathConstants.NODESET); String variable3 = (nodes.item(0).getTextContent().trim()); System.out.println("variable1 : " + variable1 + ", variable2: " + variable2 + ", variable3 : " + variable3);
Partager