Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/12/2014, 12:35
Avatar de muchuelu93
muchuelu93
 
Fecha de Ingreso: noviembre-2013
Mensajes: 35
Antigüedad: 10 años, 5 meses
Puntos: 2
Exclamación DOM crear un .xml a partir de un .dat ERROR

Hola gernte, tengo este codigo que teoricamente tendria que crear un xml a partir del .dat que le doy, pero no se por que se va directamente al CATCH y da un error de EOFException

Espero que alguien vea el error :/


Código HTML:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.*;

public class Ex5 
{
	public static void main(String[] args) throws IOException
	{
		
		File fichero = new File("Empleat.dat");
		RandomAccessFile file = new RandomAccessFile(fichero, "r");
		
		int pos=0, id, dep;
		Double salario;
		char apellido[] = new char[10], aux;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		try
		{
			DocumentBuilder bui = factory.newDocumentBuilder();
			DOMImplementation imp = bui.getDOMImplementation();
			Document doc = imp.createDocument(null, "persona", null);
			//Indiquem la versio del nostre xml
			doc.setXmlVersion("1.0");
			
			for(;;)
			{
				//ens posicionem
				file.seek(pos);
				id=file.readInt();
				for(int i=0; i<apellido.length; i++)
				{
					aux= file.readChar();
					apellido[i]=aux;
				}
				String apellidoS = new String(apellido);
				dep=file.readInt();
				salario=file.readDouble();
				
				if(id<0)
				{
					Element raiz = doc.createElement("empleado");
					
					doc.getDocumentElement().appendChild(raiz);
					CrearElemento("ID: ", Integer.toString(id), raiz, doc);
					CrearElemento("Apellido: ", apellidoS.trim(), raiz, doc);
					CrearElemento("salario: ",Double.toString(salario),raiz, doc);
				}
				
				pos = pos + 36;
				if(file.getFilePointer() == file.length()) break;
			}
			Source source = new DOMSource(doc);
			Result result = new StreamResult (new java.io.File("Empleados.xml"));
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.transform(source, result);
		}
		catch (Exception e)
		{
			System.out.print("Error: "+e);
		}
		file.close();
	}
	
	
	static void CrearElemento (String datoEmple, String valor, Element raiz, Document document)
	{
		Element elem = document.createElement(datoEmple);
		Text text = document.createTextNode(valor);
		raiz.appendChild(elem);
		elem.appendChild(text);
	}
}