user2083473
user2083473

Reputation: 47

Java error saving xml

I have a Document object in java and I want to save it, so I have this code:

TransformerFactory fabTransformador = TransformerFactory.newInstance();

Transformer transformador = fabTransformador.newTransformer();

Source origin = new DOMSource(documentoXML);

Result destino = new StreamResult(new java.io.File(nombrearchivo));

transformador.transform(origin, destino);

where "nombrearchivo" is the file name (file.xml) and documentoXMLis the Document object. When I execute the code, I receive as output: ERROR: '' I don't receive any exception, just the message ERROR: '' The document is about 1,3 GB, I don't know it it is the problem, and in that case, is there another way to save the file?

I use the next imports:

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

Upvotes: 0

Views: 291

Answers (2)

Ian Roberts
Ian Roberts

Reputation: 122364

Your code looks fine, but for reference there's an alternative approach you can use for saving a DOM document:

import org.w3c.dom.ls.*;
import java.io.FileOutputStream;

DOMImplementationLS ls = (DOMImplementationLS)documentoXML.getImplementation();
LSSerializer ser = ls.createLSSerializer();
LSOutput out = ls.createLSOutput();
out.setEncoding("UTF-8");
FileOutputStream output = new FileOutputStream("output.xml");
try {
  out.setByteStream(output);
  ser.write(documentoXML, out);
} finally {
  output.close();
}

Upvotes: 1

Raman
Raman

Reputation: 885

Use following to determine exact reason:

try { 
 secure.generaTextoXML(); 
} catch (Trowable thr) { 
 thr.printStackTrace();
} 

... or try Level.ERROR instead of Level.SEVERE for your logger:

try { 
 secure.generaTextoXML(); 
} catch (TransformerConfigurationException ex) {
 Logger.getLogger(GUI.class.getName()).log(Level.ERROR, null, ex); 
} catch (TransformerException ex) { 
 Logger.getLogger(GUI.class.getName()).log(Level.ERROR, null, ex); 
}

Upvotes: 0

Related Questions