• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Use RandomAccessFile to insert the XML declaration at the top of the file

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an invalid XML since it does not include the expected XML declaration. I have used RandomAccessFile to insert the declaration at index 0, but I do not seem to be able to retain the already existing XML content. Here is my code:

   

Where I am going wrong? Is my code incorrect?

Thank you,

G
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With RandomAccessFile, you cannot insert data except at the end. For all other positions it will overwrite what's already there.

I'd say you can go two ways to solve this:
1) The easy way:
1a) Write the XML declaration to a temporary file.
1b) Copy all remaining data from the XML file to the temporary file.
1c) Delete the original file.
1d) Move (rename) the temporary file to the original one.

2) A bit more complex:
2a) Find out how many bytes the XML declaration is.
2b) Shift everything you have up by that many bytes. You'll have to ensure that you don't overwrite existing data before it's copied though.
2c) Write the XML declaration at the start.


Both have a risk that you lose your original file. With the first way, that can only happen if 1a through 1c succeed but 1d fails. Note that you'll still have the data, just in a different folder. With the second way, it can happen at basically any moment of time. You're modifying your file, and if at any time an error occurs you're file is corrupted.

Because of the impact of failures and the complexity involved, I'd simply go for the first solution.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.io.SequenceInputStream class would let you combine a stream with the missing declaration with a stream from your XML file
to produce a stream readable by your parser combining the two.

Bill
.
 
Marshal
Posts: 25798
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Geane Norm wrote:I have an invalid XML since it does not include the expected XML declaration



Well, maybe there's some specific requirement which applies to your XML document which makes the prolog necessary, but in general it isn't necessary to prefix an XML document with a declaration.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic