title: Add new attributes to an existing XML node in java?
body: <p>I want to add an attribute to an existing xml node.I don't want to add new elements (new nodes) to my xml file, I just want to add a new attribute. How can I do this?</p>
  
  <p>In particular I've tried this lines of code:</p>
  
  <pre><code>Element process = doc.getElementsById("id");
      process.setAttribute("modelgroup", "");
  
  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer();
  DOMSource source = new DOMSource(doc);
  StreamResult result = new StreamResult(new  File("C:\\Users\\Blerta\\workspaceKEPLER\\XML_to_JSON\\SampleExample.xml"));
  transformer.transform(source, result);
  </code></pre>
  
  <p>But I get the following exception:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at Main.appendAttributes(Main.java:172)
      at Main.displayNodes(Main.java:65)
      at Main.displayNodes(Main.java:138)
      at Main.main(Main.java:42)**
  </code></pre>