-1

i have large XML Files over 100MB and more.I use this code.

        StringWriter writer = new StringWriter();
        DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = domFact.newDocumentBuilder();
        DOMSource domSource = new DOMSource(document);
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        return writer.toString();

But it doesnt work for me. I want to save my file in Database as CLOB.It is SQL Database. Is there any way? How can i save my file.

ceeleP
  • 21
  • 1
  • what doesnt work? whats your code to write into database? – metar Nov 03 '15 at 10:15
  • Exception in thread "main" java.lang.OutOfMemoryError: Java heap space. i think., i need like this -Xmx1024m. – ceeleP Nov 03 '15 at 10:43
  • when you use DOM, the whole XML will be read into your memory which will throw you this exception once the maximum has been reached. Is CLOB a requirement? Insead you could use BLOB or bytea (if you use postgresql) – metar Nov 03 '15 at 11:14
  • thank you for your answer. I need to find an other way to do it. I cant to convert to Blob because of old Data. – ceeleP Nov 03 '15 at 11:51
  • instead of using DOM, use stax or sax. take a look [here](http://stackoverflow.com/questions/21019509/java-parsing-xml-files-dom-sax-or-stax) – metar Nov 03 '15 at 12:34
  • If you're just trying to dump it into a database CLOB field, do you even need to parse it? What is the type of `document`? – Ian McLaird Nov 05 '15 at 22:19

2 Answers2

0

It is not necessary, to load the big file into your application just to write it into a database. Let the RDBMS do this directly (this is also faster...)

You did not state your RDBMS, just "SQL Database"...

If it's SQL Server a code like this should help you:

DECLARE @yourXML XML=
(
SELECT CONVERT(XML, BulkColumn,2)
FROM OPENROWSET(BULK 'PathToXmlFile.xml', SINGLE_BLOB) AS x
);
SELECT @yourXML;
--And if you really need it as text (=your CLOB) you just convert it once again:
SELECT CONVERT(VARCHAR(MAX),@yourXML) AS YourText

You can create a stored procedure around this and call it from your application.

Shnugo
  • 66,100
  • 9
  • 53
  • 114
0

If you're using a PreparedStatement (and you should be...), you can use the setClob method.

// assuming document is a File, or a string containing a filename
st.setClob(index, new FileReader(document));

If document is something other than a File, then if you can open it as an InputStream you can instead use an InputStreamReader.

Ian McLaird
  • 5,507
  • 2
  • 22
  • 31