CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: JDOM and GZIP?

  1. #1
    Join Date
    Feb 2012
    Posts
    3

    JDOM and GZIP?

    I'm trying to save a JDOM document as gzipped XML, but am getting a "JDOMParceException: Premature end of file" when I try to load the document back into my application. Any suggestions? I know that the document is well formed, because I can save and reload it successfully if I don't try to gzip it.

    Here's a snippet showing how I'm currently creating the compressed file:

    Code:
     
    private Document _doc;                                              // Note: Document creation code not included here
    
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());  // I've tried with and without this statement
    
    FileOutputStream fos = new FileOutputStream(absPath);
    GZIPOutputStream gzos = new GZIPOutputStream(fos);
    xmlOutput.output(_doc, gzos);
    and here's my attempt to load the file back into my application:

    Code:
     
    SAXBuilder parser =  new SAXBuilder();
    FileInputStream fis = new FileInputStream(absPath);
    GZIPInputStream gzis = new GZIPInputStream(fis);
    _doc = parser.build(gzis);
    Thank you

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JDOM and GZIP?

    I have never used JDOM so can't be sure what the problem is but the code shown does not close the output stream which means the stream may not be being flushed before you try to read it in again.

    Whenever you use streams you should always wrap the code in a try-catch clause and close the stream in the finally section. This is the only way you can guarantee you will not leave a stream open no matter what happens in the code, even if an exception is thrown.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Feb 2012
    Posts
    3

    Smile Re: JDOM and GZIP?

    Thanks! That solved it ...and compressed my test document from 9.9 MB to 376.4 KB

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JDOM and GZIP?

    Glad it hear it's working and it looks like it was certainly worth the effort involved in compressing it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured