• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Append in a Encoder created file

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using XMLEncoders and XMLDecoders for storage of java objects. Currently I've tried on simple data types only. But the problem is if I want to operate on the XMLEncoder created file from two different programs, then it is giving errors. In one program I'm executing
XMLEncoder e = new XMLEncoder(new BufferedOutputStream
(new FileOutputStream("polledobjects.xml")));
e.writeObject(...);
e.writeObject(...);
e.close();
and in another program I want to append to append a few more objects in the same polledobjects.xml, so I created another XML encoder object and opened the polledobjects.xml in append mode.
XMLEncoder e1 = new XMLEncoder(new BufferedOutputStream(new
FileOutputStream("polledobjects.xml",true)));
e1.writeObject(...);
e1.writeObject(...);
e1.close();
Now, Decoder works fine till the point it goes thru xml portion created by the first program, but as soon as it reached xml portion created by the second program, it gives the error->
java.lang.ArrayIndexOutOfBoundsException: 2
at java.beans.ObjectHandler.dequeueResult(XMLDecoder.java:272)
at java.beans.XMLDecoder.readObject(XMLDecoder.java:150)
at Convert.main(Convert.java:57)
XML file created is :-
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2" class="java.beans.XMLDecoder">
<object class="Data_to_Poll">
<void property="active">
<boolean>true</boolean>
</void>
<void property="agent">
<string>192.9.200.151</string>
</void>
</object>
</java>
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2" class="java.beans.XMLDecoder">
<object class="Data_to_Poll">
<void property="active">
<boolean>true</boolean>
</void>
<void property="agent">
<string>192.9.200.151</string>
</void>
</object>
</java>
I know it is giving problems b'cos it is encountering <?xml tag 2 times in the same xml document, but what should I do if I want to append in the same xml file and in the second program nothing more than filename is known...
plz. help me out....
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if you could emit the second <?xml?> prolog, your document would be malformed XML because only one root element is allowed. In other words, you'll need to combine the documents in-memory and only then write the resulting document to disk.
 
It runs on an internal combustion engine. This ad does not:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic