XML : update xml document using 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 update the rating of "jojo" restaurant in jbeil from 4.3/5 to 4.5/5 using xpath and netbeans please help ,

this code give the rating ,

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/Restaurant";                 NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);       for (int i = 0; i < nodeList.getLength(); i++) {          Node nNode = nodeList.item(i);          System.out.println("\nCurrent Element :"              + nNode.getNodeName());          if (nNode.getNodeType() == Node.ELEMENT_NODE) {             Element eElement = (Element) nNode;               System.out.println("rating : "                 + eElement                   .getElementsByTagName("rating")                   .item(0)                   .getTextContent());    

and i want only to update the rating of restaurant in jbeil where name is "jojo" , please help

No comments:

Post a Comment