Comment faire pour supprimer les lignes vides à partir du fichier XML?

En bref, j'ai beaucoup de lignes vides générés dans un fichier XML, et je suis à la recherche d'un moyen de les éliminer comme un moyen de se pencher le fichier. Comment puis-je le faire ?

Pour une explication détaillée; j'ai actuellement ce fichier XML :

<recent>
  <paths>
    <path>path1</path>
    <path>path2</path>
    <path>path3</path>
    <path>path4</path>
  </paths>
</recent>

Et j'utilise ce code Java pour supprimer toutes les balises, et en ajouter de nouveaux à la place :

public void savePaths( String recentFilePath ) {
    ArrayList<String> newPaths = getNewRecentPaths();
    Document recentDomObject = getXMLFile( recentFilePath );  //Get the <recent> element.
    NodeList pathNodes = recentDomObject.getElementsByTagName( "path" );   //Get all <path> nodes.

    //1. Remove all old path nodes :
        for ( int i = pathNodes.getLength() - 1; i >= 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 <paths> node.

        for( String newPath: newPaths ) {
            Element newPathElement = recentDomObject.createElement( "path" );
            newPathElement.setTextContent( newPath );
            pathsElement.appendChild( newPathElement );
        }

    //3. Save the XML changes :
        saveXMLFile( recentFilePath, recentDomObject ); 
}

Après l'exécution de cette méthode un certain nombre de fois, j'obtiens un fichier XML avec de bons résultats, mais avec de nombreuses lignes vides après les "chemins" de la balise et avant le premier "chemin" de la balise, comme ceci :

<recent>
  <paths>





    <path>path5</path>
    <path>path6</path>
    <path>path7</path>
  </paths>
</recent>

Quelqu'un sait comment résoudre ce problème ?

------------------------------------------- Edit: Ajout de la getXMLFile(...), saveXMLFile(...) du 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();
            }
    }
}
Il pourrait être utile de voir le contenu de votre saveXMLFile méthode.
Bien sûr, j'ai édité la question.
Vous pourriez avoir un coup d'oeil à la Suppression de Nœuds et les Lignes Vides dans XML à l'Aide de Java et stackoverflow.com/questions/7190639/...

OriginalL'auteur Brad | 2012-10-01