Está en la página 1de 2

D:\Codigo Fuente\Java\Depurado\Remover Espacios En Blanco XML\RemoverEspaciosEnBlancoXML.

java

/**
*
**/
import
import
import
import

jueves, 30 de junio de 2016 07:41 p.m.

removeWSNode.java
org.w3c.dom.*;
com.ibm.xml.parser.*;
java.io.*;
java.util.*;

public class RemoverEspaciosEnBlancoXML {


public static void main ( String[] argv ) {
if ( argv.length != 1 ) {
System.err.println ( "Require a filename." );
System.exit ( 1 );
}
try {
// Open specified file.
FileInputStream is = new FileInputStream ( argv[0] );
// Start to parse
Parser parser = new Parser ( argv[0] );
Document doc = parser.readStream ( is );
// Error?
if ( 0 < parser.getNumberOfErrors ( ) ) {
System.exit ( 1 );
}
Element root = doc.getDocumentElement ( );
removerNodosEnBlanco ( root );
((TXDocument )doc ).print ( new PrintWriter ( System.out ) );
// Impresin en un archivo
// Definicin del archivo de salida
PrintStream archivo = new PrintStream ( new BufferedOutputStream ( new
FileOutputStream ( "salidaPizarraElectronica.xml" ) ) );
// Guardar el documento XML en el archivo
((TXDocument)doc).print ( new PrintWriter ( archivo ) );
// Vaciar el buffer
archivo.flush ( );
// Cierre del archivo de salida
archivo.close ( );
} catch ( Exception e ) {
e.printStackTrace ( );
}
}
public static void removerNodosEnBlanco ( Element parent ) {
Node nextNode = parent.getFirstChild ( );
for ( Node child = parent.getFirstChild ( );
nextNode != null; ) {
child = nextNode;
nextNode = child.getNextSibling ( );
if ( child instanceof TXText ) {
-1-

D:\Codigo Fuente\Java\Depurado\Remover Espacios En Blanco XML\RemoverEspaciosEnBlancoXML.java

if ( ((TXText)child).getIsIgnorableWhitespace ( ) ) {
parent.removeChild ( child );
}
}
else if ( child instanceof TXElement ) {
removerNodosEnBlanco ( (Element )child );
}
}
}
}

-2-

jueves, 30 de junio de 2016 07:41 p.m.

También podría gustarte