XML : update xml with xpath

Hello this is my xml document :

   <city>      <beirut>     <restaurant>       <name>sada</name>     </restaurant>      </beirut>       <jbeil>     <restaurant>       <name>toto</name>        <rating>4.3/5</rating>     </restaurant>      <restaurant>       <name>jojo</name>        <rating>4.3/5</rating>     </restaurant>      </jbeil>       <sour>     <restaurant>       <name>sada</name>     </restaurant>      </sour>      </city>    

I want to change the rating of jojo restaurant only , i tried this code but didn't work please help

                   try {       File inputFile = new File("src/xpath/josephXml.xml");       DocumentBuilderFactory dbFactory           = DocumentBuilderFactory.newInstance();       DocumentBuilder dBuilder;         dBuilder = dbFactory.newDocumentBuilder();         Document doc = dBuilder.parse(inputFile);       doc.getDocumentElement().normalize();         XPath xPath =  XPathFactory.newInstance().newXPath();         String expression = "/City/Jbeil/Restaurants/Restaurant[name='Feniqia']/rating";     

Element e = (Element)xPath.evaluate(expression, doc, XPathConstants.NODE);

if (e != null){ e.setTextContent("5/5");

}

   } catch (ParserConfigurationException | SAXException | IOException e) {    }    

No comments:

Post a Comment