Foros del Web » Programación para mayores de 30 ;) » Programación General »

informacion sobre XML y java

Estas en el tema de informacion sobre XML y java en el foro de Programación General en Foros del Web. hola, hay alguna pagina web que este bien sobre este tema. Me gustaria un ejemplo de carga de un fichero xml pero todavia no lo ...
  #1 (permalink)  
Antiguo 09/08/2004, 16:08
 
Fecha de Ingreso: marzo-2004
Mensajes: 96
Antigüedad: 20 años, 2 meses
Puntos: 0
informacion sobre XML y java

hola,
hay alguna pagina web que este bien sobre este tema. Me gustaria un ejemplo de carga de un fichero xml pero todavia no lo he encontrado

gracias
  #2 (permalink)  
Antiguo 11/08/2004, 06:12
Avatar de Helbira  
Fecha de Ingreso: octubre-2001
Ubicación: Sevilla, España
Mensajes: 1.228
Antigüedad: 22 años, 6 meses
Puntos: 5
Información Código Java que lee un XML

Hola Davinia.
Igual que tú me costó muchísimo encontrar un ejemplo que leyera un XML desde un archivo con Java hasta que encontré el siguiente código:

Código PHP:
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.*;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MiEcho extends DefaultHandler
{
 public static void main(String argv[])
 {
  if (argv.length != 1) {
   System.err.println("Usage: cmd filename");
   System.exit(1);
  }
  // Use an instance of ourselves as the SAX event handler
  DefaultHandler handler = new MiEcho();
  // Use the default (non-validating) parser
  SAXParserFactory factory = SAXParserFactory.newInstance();
  try {
   // Set up output stream
   out = new OutputStreamWriter(System.out, "UTF8");
   // Parse the input
   SAXParser saxParser = factory.newSAXParser();
   saxParser.parse( new File(argv[0]), handler );
  } catch (Throwable t) {
   t.printStackTrace();
  }
  System.exit(0);
 }
 static private Writer out;
 
 public void startDocument() throws SAXException
 {
  emit("<?xml version="1.0\" encoding=\"UTF-8\"?>");
  
nl();
 }
 public 
void endDocument() throws SAXException
 
{
  try {
   
nl();
   
out.flush();
  } catch (
IOException e) {
   throw new 
SAXException("I/O error"e);
  }
 }
 public 
void startElement(String namespaceURI,
        
String sName// simple name (localName)
        
String qName// qualified name
        
Attributes attrsthrows SAXException
 
{
  
String eName sName// element name
  
if ("".equals(eName)) 
    
eName qName// namespaceAware = false
  
emit("<"+eName);
  if (
attrs != null) {
   for (
int i 0attrs.getLength(); i++) {
    
String aName attrs.getLocalName(i); // Attr name
    
if ("".equals(aName))
      
aName attrs.getQName(i);
    
emit(" ");
    
emit(aName+"=\""+attrs.getValue(i)+"\"");
   }
  }
  
emit(">");
 }
 public 
void endElement(String namespaceURI,
         
String sName// simple name
         
String qName  // qualified name
        
throws SAXException
 
{
  
String eName sName// element name
  
if ("".equals(eName)) 
    
eName qName// namespaceAware = false
  
emit("</"+eName+">");
 }
 public 
void characters(char buf[], int offsetint lenthrows SAXException
 
{
  
String s = new String(bufoffsetlen);
  
emit(s);
 }
 private 
void emit(String sthrows SAXException
 
{
  try {
   
out.write(s);
   
out.flush();
  } catch (
IOException e) {
   throw new 
SAXException("I/O error"e);
  }
 }
 
 private 
void nl() throws SAXException
 
{
  
String lineEnd =  System.getProperty("line.separator");
  try {
   
out.write(lineEnd);
       
  } catch (
IOException e) {
   throw new 
SAXException("I/O error"e);
  }
 }

}
Te lo he puesto a lo bruto porque no puedo adjuntar archivos pero en realidad podría pasarte el proyecto completo con las librerias y demás para que puedas verlo funcionar del tirón antes de ponerte a destripar el código.

Ese ejemplo concretamente lo que hace es recorrer un fichero XML completo que pasas por línea de comando y testea si sintácticamente es un XML válido. Utiliza SAX (seguro que te suena).

Si te interesa el proyecto te lo envio por correo electrónico pero necesito tu dirección.

Un beso
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:21.