title: NO_MODIFICATION_ALLOWED_ERR: An attempt is made to modify an object where modifications are not allowed
body: <p>Hello I am getting NO modification allowed when trying to add
   a new node to an xml file and I am not sure why because I am using the same code for another file and it works fine, here is the code:</p>
  
  <pre><code> public void addStockItem(String itemStr, int qty){
      String path = System.getProperty("user.dir") + "/src/stock.xml";
        try {
      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
          docFactory.setIgnoringElementContentWhitespace(true);
  
          DocumentBuilder docBuilder;      
  
          docBuilder = docFactory.newDocumentBuilder();
  
          Document doc = docBuilder.parse(path);
  
          Node root = doc.getFirstChild();
  
          Node item = doc.createElement("item");
          item.setTextContent(itemStr);
          NamedNodeMap itemAttr = item.getAttributes();
          Attr qtyAttr = doc.createAttribute("quantity");
          qtyAttr.setTextContent(qty+"");
          itemAttr.setNamedItem(qtyAttr);
          root.appendChild(item);
  
          Transformer transformer = TransformerFactory.newInstance().newTransformer();
          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  
          //initialize StreamResult with File object to save to file
          StreamResult result = new StreamResult(new StringWriter());
          DOMSource source = new DOMSource(doc);
          transformer.transform(source, result);
          String xmlString = result.getWriter().toString();
  
          BufferedWriter out = new BufferedWriter(new FileWriter(path));
          out.write(xmlString);
          out.close();
  
      } catch (Exception ex) {
          System.out.print(ex);
  
      }
  
  }
  </code></pre>
  
  <p>My xml is the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;!DOCTYPE stock SYSTEM "barstock.dtd"&gt;
  
  &lt;stock&gt;
     &lt;item quantity="23"&gt;dark rum&lt;/item&gt;
     &lt;item quantity="5"&gt;light rum&lt;/item&gt;
     &lt;item quantity="4"&gt;vodka&lt;/item&gt;
     &lt;item quantity="2"&gt;brandy&lt;/item&gt;
     &lt;item quantity="3"&gt;orange juice&lt;/item&gt;
     &lt;item quantity="2"&gt;cream&lt;/item&gt;
     &lt;item quantity="2"&gt;dry vermouth&lt;/item&gt;
    &lt;item quantity="7"&gt;amaretto&lt;/item&gt;
  &lt;/stock&gt;
  </code></pre>
  
  <p>With this DTD:</p>
  
  <pre><code>&lt;!-- DTD for bar stock (drink ingredients --&gt;
   &lt;!-- One day we should add a way to allow quantities and units to be included --&gt;
  
       &lt;!ELEMENT stock (item)*&gt;
  
       &lt;!ELEMENT item  (#PCDATA)&gt;
       &lt;!ATTLIST item quantity CDATA #IMPLIED&gt;
  </code></pre>
  
  <p>Thanks for the help.</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> I (Amir Rachum) am adding my own stacktrace to the question as I have this problem as well:</p>
  
  <pre><code>org.w3c.dom.DOMException: NO_MODIFICATION_ALLOWED_ERR: An attempt is made to modify an object where modifications are not allowed.
      at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(Unknown Source)
      at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(Unknown Source)
      at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(Unknown Source)
      at cs236369.hw5.servlets.xml.UploadTheme.addThemeToList(UploadTheme.java:115)
      at cs236369.hw5.servlets.xml.UploadTheme.doPost(UploadTheme.java:91)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>