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.

3
  • Maybe this post could help you? stackoverflow.com/questions/6124239/… Commented Aug 13, 2015 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). Commented Aug 13, 2015 at 14:21
  • @FatalError can u give me an example how to write that value back on the file using xpath? Commented Aug 13, 2015 at 14:31

1 Answer 1

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. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.