title: java dom modify xml file
body: <p>I want to add some nodes into an existing xml file.
  The xml file structure looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt;&lt;?xml-stylesheet type="text/xsl" href="new2.xsl"?&gt;&lt;patients&gt;
      &lt;patient&gt;
      &lt;stoixeia_astheni&gt;
          &lt;arithmos_eksetasis&gt;1&lt;/arithmos_eksetasis&gt;
          &lt;imerominia_eksetasis&gt;11/12/2005&lt;/imerominia_eksetasis&gt;
          &lt;amka&gt;14385&lt;/amka&gt;
          &lt;surname&gt;aaa&lt;/surname&gt;
          &lt;name&gt;aaa&lt;/name&gt;
          &lt;onoma_patros&gt;aaa&lt;/onoma_patros&gt;
          &lt;imgennisis&gt;15/03/1984&lt;/imgennisis&gt;
          &lt;diagnosi&gt;aaa&lt;/diagnosi&gt;
          &lt;famagogi&gt;depon&lt;/famagogi&gt;
      &lt;/stoixeia_astheni&gt;
      &lt;stoixeia_epikoinonias&gt;
          &lt;dieuthinsi&gt;aaa&lt;/dieuthinsi&gt;
          &lt;takodikas&gt;11474&lt;/takodikas&gt;
          &lt;perioxi&gt;aaa&lt;/perioxi&gt;
          &lt;stathero&gt;2106425246&lt;/stathero&gt;
          &lt;kinito&gt;-&lt;/kinito&gt;
      &lt;/stoixeia_epikoinonias&gt;
      &lt;loipa_stoixeia&gt;
          &lt;fylo&gt;aaa&lt;/fylo&gt;
          &lt;oikkatastasi&gt;aaa&lt;/oikkatastasi&gt;
          &lt;epaggelma&gt;aaa&lt;/epaggelma&gt;
          &lt;istoriko&gt;-&lt;/istoriko&gt;
          &lt;sxolia&gt;-&lt;/sxolia&gt;
      &lt;/loipa_stoixeia&gt;
      &lt;/patient&gt;
      &lt;patient&gt;
      &lt;stoixeia_astheni&gt;
          &lt;arithmos_eksetasis&gt;2&lt;/arithmos_eksetasis&gt;
          &lt;imerominia_eksetasis&gt;12/12/2005&lt;/imerominia_eksetasis&gt;
          &lt;amka&gt;14325&lt;/amka&gt;
          &lt;surname&gt;aaa&lt;/surname&gt;
          &lt;name&gt;aaa&lt;/name&gt;
          &lt;onoma_patros&gt;aaa&lt;/onoma_patros&gt;
          &lt;imgennisis&gt;15/03/1984&lt;/imgennisis&gt;
          &lt;diagnosi&gt;aaa&lt;/diagnosi&gt;
          &lt;famagogi&gt;depon&lt;/famagogi&gt;
      &lt;/stoixeia_astheni&gt;
      &lt;stoixeia_epikoinonias&gt;
          &lt;dieuthinsi&gt;aaa&lt;/dieuthinsi&gt;
          &lt;takodikas&gt;11474&lt;/takodikas&gt;
          &lt;perioxi&gt;aaa&lt;/perioxi&gt;
          &lt;stathero&gt;2106425246&lt;/stathero&gt;
          &lt;kinito&gt;-&lt;/kinito&gt;
      &lt;/stoixeia_epikoinonias&gt;
      &lt;loipa_stoixeia&gt;
          &lt;fylo&gt;aaa&lt;/fylo&gt;
          &lt;oikkatastasi&gt;aaa&lt;/oikkatastasi&gt;
          &lt;epaggelma&gt;aaa&lt;/epaggelma&gt;
          &lt;istoriko&gt;-&lt;/istoriko&gt;
          &lt;sxolia&gt;-&lt;/sxolia&gt;
      &lt;/loipa_stoixeia&gt;
      &lt;/patient&gt;
  &lt;/patients&gt;
  </code></pre>
  
  <p>I am trying something like:</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      String[] elem;
      elem = new String[20];
      elem [1]="arithmos_eksetasis";
      elem [2]="imerominia_eksetasis";
      elem [3]="amka";
      elem [4]="surname";
      elem [5]="name";
      elem [6]="onoma_patros";
      elem [7]="imgennisis";
      elem [8]="diagnosi";
      elem [9]="famagogi";
      elem [10]="dieuthinsi";
      elem [11]="takodikas";
      elem [12]="perioxi";
      elem [13]="stathero";
      elem [14]="kinito";
      elem [15]="fylo";
      elem [16]="oikkatastasi";
      elem [17]="epaggelma";
      elem [18]="istoriko";
      elem [19]="sxolia";
  
      try {
          String filepath = "C:\\Users\\Chris\\Desktop\\tsiou\\workspace\\askhsh3\\WebContent\\askisi3.xml";
  
          DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
          Document doc = docBuilder.parse(filepath);
  
  
          //Node staff = doc.getElementsByTagName("staff").item(0);
  
  
          // append a new node to staff
          Document doc2 = docBuilder.newDocument();
          Element patient = doc2.createElement("patient");
  
          Element st_as = doc2.createElement("stoixeia_astheni");
          for(int i=1;i&lt;=9;i++){
              Element tmp= doc2.createElement(elem[i]);
              tmp.appendChild(doc2.createTextNode("aaa"));
              st_as.appendChild(tmp);
          }
          Element st_ep = doc2.createElement("stoixeia_epikoinonias");
          for(int i=10;i&lt;=15;i++){
              Element tmp= doc2.createElement(elem[i]);
              tmp.appendChild(doc2.createTextNode("aaa"));
              st_ep.appendChild(tmp);     
          }
          Element st_lp = doc2.createElement("loipa_stoixeia");
          for(int i=16; i&lt;=19;i++){
              Element tmp= doc2.createElement(elem[i]);
              tmp.appendChild(doc2.createTextNode("aaa"));
              st_lp.appendChild(tmp);     
          }
          patient.appendChild(st_as);
          patient.appendChild(st_ep);
          patient.appendChild(st_lp);
          doc.importNode(patient,true);
  
          TransformerFactory tf = TransformerFactory.newInstance();
          Transformer trans = tf.newTransformer();
          OutputStream out = new FileOutputStream(filepath);
          trans.transform(new DOMSource(doc), new StreamResult(out));
  
         } catch (ParserConfigurationException pce) {
          pce.printStackTrace();
         } catch (TransformerException tfe) {
          tfe.printStackTrace();
         } catch (IOException ioe) {
          ioe.printStackTrace();
         } catch (SAXException sae) {
          sae.printStackTrace();
         }
  
  
      response.setContentType("text/html");
      PrintWriter pw = response.getWriter();
  
  }
  </code></pre>
  
  <p>The result is with no luck (the xml doesn't changed).</p>
  
  <p>When i am trying <code>trans.transform(new DOMSource(patient), new StreamResult(out));</code> i can see the new patient..i also tried <code>doc.adoptNode(patient)</code></p>
  
  <p>Any advice would be usefull</p>