title: How can I update a file using SAXParser in Java
body: <p>I am using SAX parser to read data from the xml. Now I want to modify the data using SAX parser. how can I do that?</p>
  
  <p>My XML file is,</p>
  
  <pre><code>&lt;ProductCatalog&gt;
      &lt;item id="w1" type="SmartWatches"&gt;
          &lt;name&gt;Apple Watch&lt;/name&gt;
          &lt;price&gt;400&lt;/price&gt;
          &lt;image&gt;abc.jpg&lt;/image&gt;
          &lt;manufacturer&gt;Apple&lt;/manufacturer&gt;
          &lt;condition&gt;New&lt;/condition&gt;
          &lt;discount&gt;10&lt;/discount&gt;
          &lt;accessories&gt;
              &lt;accessory&gt;charger&lt;/accessory&gt;
              &lt;accessory&gt;belt&lt;/accessory&gt;
          &lt;/accessories&gt;
      &lt;/item&gt;
  
      &lt;item id="w2" type="SmartWatches"&gt;
          &lt;name&gt;Apple Watch&lt;/name&gt;
          &lt;price&gt;400&lt;/price&gt;
          &lt;image&gt;abc.jpg&lt;/image&gt;
          &lt;manufacturer&gt;Apple&lt;/manufacturer&gt;
          &lt;condition&gt;New&lt;/condition&gt;
          &lt;discount&gt;10&lt;/discount&gt;
          &lt;accessories&gt;
              &lt;accessory&gt;charger&lt;/accessory&gt;
              &lt;accessory&gt;belt&lt;/accessory&gt;
          &lt;/accessories&gt;
      &lt;/item&gt;
  &lt;/ProductCatalog&gt;
  </code></pre>
  
  <p>And also this is my implementation of SAX parser in java.</p>
  
  <pre><code>public class UserHandler extends DefaultHandler {
      private Item item;
      private String value;
      private String filePath;
      private Map&lt;String, Item&gt; map = new HashMap&lt;String, Item&gt;();
  
      UserHandler(String filePath) {
          this.filePath = filePath;
          parseDocument();
          printValue();
      }
  
      private void printValue() {
          Set keys = map.keySet();
          for (Iterator i = keys.iterator(); i.hasNext();) {
              String key = (String) i.next();
              System.out.println("Key : " + key);
  
              Item itemDemo = map.get(key);
              System.out.println("Name : " + itemDemo.getName());
              System.out.println("Price : " + itemDemo.getPrice());
              System.out.println("Image : " + itemDemo.getImage());
              System.out.println("Manufacturer : " + itemDemo.getManufacturer());
              System.out.println("Condition : " + itemDemo.getCondition());
              System.out.println("Discount : " + itemDemo.getDiscount());
              System.out.println("Accessories : " + itemDemo.getAccessories().toString());
          }
      }
  
      private void parseDocument() {
          SAXParserFactory factory = SAXParserFactory.newInstance();
          try {
              SAXParser parser = factory.newSAXParser();
              parser.parse(filePath, this);
          } catch (ParserConfigurationException e) {
              System.out.println("ParserConfig error");
          } catch (SAXException e) {
              System.out.println("SAXException : xml not well formed");
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
          if (qName.equalsIgnoreCase("item")) {
              item = new Item();
              item.setId(attributes.getValue("id"));
              item.setType(attributes.getValue("type"));
          }
      }
  
      public void endElement(String uri, String localName, String qName) throws SAXException {
          if (qName.equals("item")) {
              map.put(item.getId(), item);
              return;
          }
          if (qName.equalsIgnoreCase("name")) {
              item.setName(value);
              return;
          }
          if (qName.equalsIgnoreCase("price")) {
              item.setPrice(value);
              return;
          }
          if (qName.equalsIgnoreCase("image")) {
              item.setImage(value);
              return;
          }
          if(qName.equalsIgnoreCase("manufacturer")){
              item.setManufacturer(value);
              return;
          }
          if(qName.equalsIgnoreCase("condition")){
              item.setCondition(value);
              return;
          }
          if(qName.equalsIgnoreCase("discount")){
              item.setDiscount(value);
              return;
          }
          if(qName.equalsIgnoreCase("accessory")){
              item.getAccessories().add(value);
              return;
          }
      }
  
      public void characters(char[] ch, int start, int length) throws SAXException {
          value = new String(ch, start, length);
      }
  }
  </code></pre>
  
  <p>How can I modify a particular attribute in XML file? The changes should be replicated in file also. I cannot use DOM parser. </p>