title: How to remove extra empty lines from XML file?
body: <p>In short; i have many empty lines generated in an XML file, and i am looking for a way to remove them as a way of leaning the file. How can i do that ?</p>
  
  <p>For detailed explanation; I currently have this XML file :</p>
  
  <pre><code>&lt;recent&gt;
    &lt;paths&gt;
      &lt;path&gt;path1&lt;/path&gt;
      &lt;path&gt;path2&lt;/path&gt;
      &lt;path&gt;path3&lt;/path&gt;
      &lt;path&gt;path4&lt;/path&gt;
    &lt;/paths&gt;
  &lt;/recent&gt;
  </code></pre>
  
  <p>And i use this Java code to delete all  tags, and add new ones instead :</p>
  
  <pre><code>public void savePaths( String recentFilePath ) {
      ArrayList&lt;String&gt; newPaths = getNewRecentPaths();
      Document recentDomObject = getXMLFile( recentFilePath );  // Get the &lt;recent&gt; element.
      NodeList pathNodes = recentDomObject.getElementsByTagName( "path" );   // Get all &lt;path&gt; nodes.
  
      //1. Remove all old path nodes :
          for ( int i = pathNodes.getLength() - 1; i &gt;= 0; i-- ) { 
              Element pathNode = (Element)pathNodes.item( i );
              pathNode.getParentNode().removeChild( pathNode );
          }
  
      //2. Save all new paths :
          Element pathsElement = (Element)recentDomObject.getElementsByTagName( "paths" ).item( 0 );   // Get the first &lt;paths&gt; node.
  
          for( String newPath: newPaths ) {
              Element newPathElement = recentDomObject.createElement( "path" );
              newPathElement.setTextContent( newPath );
              pathsElement.appendChild( newPathElement );
          }
  
      //3. Save the XML changes :
          saveXMLFile( recentFilePath, recentDomObject ); 
  }
  </code></pre>
  
  <p>After executing this method a number of times i get an XML file with right results, but with many empty lines after the "paths" tag and before the first "path" tag, like this :</p>
  
  <pre><code>&lt;recent&gt;
    &lt;paths&gt;
  
  
  
  
  
      &lt;path&gt;path5&lt;/path&gt;
      &lt;path&gt;path6&lt;/path&gt;
      &lt;path&gt;path7&lt;/path&gt;
    &lt;/paths&gt;
  &lt;/recent&gt;
  </code></pre>
  
  <p>Anyone knows how to fix that ?</p>
  
  <p><strong>------------------------------------------- Edit: Add the getXMLFile(...), saveXMLFile(...) code.</strong></p>
  
  <pre><code>public Document getXMLFile( String filePath ) { 
      File xmlFile = new File( filePath );
  
      try {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document domObject = db.parse( xmlFile );
          domObject.getDocumentElement().normalize();
  
          return domObject;
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      return null;
  }
  
  public void saveXMLFile( String filePath, Document domObject ) {
      File xmlOutputFile = null;
      FileOutputStream fos = null;
  
      try {
          xmlOutputFile = new File( filePath );
          fos = new FileOutputStream( xmlOutputFile );
          TransformerFactory transformerFactory = TransformerFactory.newInstance();
          Transformer transformer = transformerFactory.newTransformer();
          transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
          transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" );
          DOMSource xmlSource = new DOMSource( domObject );
          StreamResult xmlResult = new StreamResult( fos );
          transformer.transform( xmlSource, xmlResult );  // Save the XML file.
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (TransformerConfigurationException e) {
          e.printStackTrace();
      } catch (TransformerException e) {
          e.printStackTrace();
      } finally {
          if (fos != null)
              try {
                  fos.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      }
  }
  </code></pre>