0% found this document useful (0 votes)
56 views2 pages

Simple Parse and Print

This Java program parses an XML document from a file, checks for errors, and prints the document structure and content to the console. It takes a filename as input, opens the file, parses it into a DOM document, prints the document if there are no errors, and then extracts and prints additional information like the root node name, number of "Periodo" nodes, and their names.

Uploaded by

radutari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views2 pages

Simple Parse and Print

This Java program parses an XML document from a file, checks for errors, and prints the document structure and content to the console. It takes a filename as input, opens the file, parses it into a DOM document, prints the document if there are no errors, and then extracts and prints additional information like the root node name, number of "Periodo" nodes, and their names.

Uploaded by

radutari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

D:\Codigo Fuente\Java\Depurado\Simple Parser And Print\SimpleParseAndPrint.

java

/**
*
*/
import
import
import
import
import

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

SimpleParseAndPrint.java
com.ibm.xml.parser.Parser;
com.ibm.xml.parser.TXDocument;
java.io.FileInputStream;
java.io.PrintWriter;
org.w3c.dom.Document;

import org.w3c.dom.*;

public class SimpleParseAndPrint {


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 ( parser.getNumberOfErrors ( ) > 0 ) {
System.exit ( 1 );
// If the document has any error,
// the program is terminated.
}
// Print to the standard output
// in XML format.
((TXDocument)doc).print ( new PrintWriter ( System.out ) );
// Codes for process will be here
// Pruebas
System.out.println("\n");
// Obtener el nodo raz
Element raiz = doc.getDocumentElement();
// Imprimir el nombre del nodo raz
System.out.println("Nombre nodo raz: " + raiz.getNodeName());
System.out.println("\n");

// Lista de nodos perodo


NodeList listaNodos = doc.getElementsByTagName("Periodo");
System.out.println("Cantidad Nodos Periodo: " + listaNodos.getLength());
System.out.println("\n");
// Imprimir lista de nodos
for (int i = 0; i < listaNodos.getLength(); i++) {
System.out.println(listaNodos.item(i).getNodeName());
}
-1-

D:\Codigo Fuente\Java\Depurado\Simple Parser And Print\SimpleParseAndPrint.java

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

} catch ( Exception e ) {
e.printStackTrace ( );
}
}
}

-2-

You might also like