0

i have the following xml file:

<?xml version='1.0' encoding='utf-8' ?>
<osm>
<node id="25779111" lat="50.03342" lon="5.461545"/>
<node id="25779112" lat="50.03304" lon="5.814"/>
<node id="25779119" lat="50.03395" lon="5.8255"/>
<tag k="maxspeed" v="30"/>
<tag k="maxspeed:zone" v="yes"/>
<ele k="maxspeed:zone" v="60"/>

and i reviewed some posts to know how can i update or change a value in the xml file using xpath, and idid the following :

for (int i = 1 ; i <= 2; i++) {
    String expr0 = "//node[@lat='53.0334062'][@lon='8.8461545']/following-sibling::tag["+i+"]/@v";
    xPath.compile(expr0);
    Node s = (Node) xPath.evaluate(expr0, document, XPathConstants.NODE);
    System.out.println(s);

    s.setNodeValue("999");
}

but when i checked the xml file, i expected to see the change written to it, but there was no change. please let me know how to change the value in the xml file using xPath correctly.

rmaik
  • 1,076
  • 3
  • 15
  • 48
  • Maybe this post could help you? http://stackoverflow.com/questions/6124239/how-to-update-xml-using-xpath-and-java – Smudoo Aug 13 '15 at 14:12
  • 1
    Did you write the file back to disk? Otherwise, it sounds like you only modified the DOM (in-memory representation of the document). – FatalError Aug 13 '15 at 14:21
  • @FatalError can u give me an example how to write that value back on the file using xpath? – rmaik Aug 13 '15 at 14:31

1 Answers1

0

you need to write the document back to the disk. Here's how:

  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer();
  DOMSource source = new DOMSource(document);
  String filePath = "path/to/new/file.xml";
  StreamResult streamResult =  new StreamResult(new File(filePath));
  transformer.transform(source, streamResult);

More help here: http://download.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT4.html

Hope this helps. :)

nullvoid
  • 21
  • 6