Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The below code can be used to get the values of the ref attribute and to store them in an arraylist.</p> <pre><code>String s="&lt;?xml version='1.0' encoding='UTF-8'?&gt;&lt;osm version=\"0.6\" generator=\"Osmosis 0.43.1\"&gt; &lt;node id=\"95946773\" version=\"3\" timestamp=\"2009-11-14T15:52:46Z\" uid=\"17427\" user=\"dysteleologist\" changeset=\"3114170\" lat=\"26.2870135\" lon=\"-80.2120936\"/&gt; &lt;node id=\"95946776\" version=\"3\" timestamp=\"2010-10-23T15:07:03Z\" uid=\"207745\" user=\"NE2\" changeset=\"6146275\" lat=\"26.2870058\" lon=\"-80.2128489\"/&gt;&lt;way id=\"226137459\" version=\"1\" timestamp=\"2013-06-17T20:19:27Z\" uid=\"1119200\" user=\"wagn\" changeset=\"16595491\"&gt; &lt;nd ref=\"2349898722\"/&gt; &lt;nd ref=\"2349898723\"/&gt; &lt;nd ref=\"2349898724\"/&gt; &lt;nd ref=\"2349898725\"/&gt; &lt;nd ref=\"2349898726\"/&gt; &lt;nd ref=\"2349898730\"/&gt; &lt;nd ref=\"2349898727\"/&gt; &lt;nd ref=\"2349898728\"/&gt; &lt;nd ref=\"2349898729\"/&gt; &lt;nd ref=\"2349898722\"/&gt; &lt;tag k=\"building\" v=\"house\"/&gt; &lt;/way&gt;&lt;/osm&gt;"; DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); InputSource is = new InputSource(new StringReader(s)); DocumentBuilder builder; try { builderFactory.setIgnoringElementContentWhitespace(true); builder = builderFactory.newDocumentBuilder(); Document xml = builder.parse(is); XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression expression; expression = xPath.compile("/osm/way/nd/@ref"); NodeList ss = (NodeList) expression.evaluate(xml,XPathConstants.NODESET); ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; ss.getLength(); i++) { list.add(ss.item(i).getTextContent()); } System.out.println(list); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XPathExpressionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload