I/O using XML in Java.

Soldato
Joined
23 Dec 2010
Posts
3,483
Hey guys,

We've been asked to create a application which utalises 3 kinds of IO, text, XML and serialization.

Text and Serialization went by without any hitches, but for some reason XML refuses to work.

Quick overview of the application here.


  • Two classes, books and authors.
  • Linked by the author instance variable.
  • Method class with all of the methods inside (who would have guessed?)

This is what I've got.

Going to the method -

Code:
try {
                   writeXML("file.xml");
                }
                catch (IOException e) {
                    System.out.println("Error whilst writing file");
                }
            }
The method itself

Code:
public void writeXML(String fn) throws IOException {
        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
                new BufferedOutputStream(
                        new FileOutputStream(fn))));
        encoder.writeObject(this);
        encoder.close();
    }

I can't see anything wrong there, can you?

Edit:- Important to note that the ArrayList with the book and author objects stored is also in this class - hense the (this).
 
Nope.

Wasn't that.

This is what the created XML displays...

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_10" class="java.beans.XMLDecoder">
<object class="Model"/>
</java>
 
Hmm ... most strange. Only thing I can think of now from what you've posted is that either one of the classes doesn't implement Serializable or one of the class members doesn't have a no-argument constructor.
 
Not a lot of code or information to go on to diagnose. Could clutch at some straws but it probably won't help.
 
Code:
public void writeXML(String fn) throws IOException {
        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
                new BufferedOutputStream(
                        new FileOutputStream(fn))));
        encoder.writeObject(this);
        encoder.close();
    }

I can't see anything wrong there, can you?
In terms of the XMLEncoder, I'm not sure I can help. I assume that the Model class is in the default package?

There are a few other newbie mistakes with this code. You'll probably get away with them for uni coursework or whatever, but they would be immediately failed on QA in any software shop. You might even be aware of them but I'll detail them anyway.

You're bubbling the IOException, without cleaning up. So there are a few problems that you might encounter here:
  • If, for some reason, you cannot create a BufferedOutputStream or XMLEncoder object (i.e. the constructor throws IOException), then you will never be able to close the file
  • If the writeObject() call fails, then you do not close any of the streams.

You probably shouldn't use "this" when serializing - standard practice is to have models which don't do anything other than containing information. You then write another class which does the serialization.
 
Let me show you how I've done my seriazliable, it works without any hitches whatsoever.

Code:
    public void write(String fn) throws IOException{
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fn));
        oos.writeObject(this);
        oos.close();
    }
 
Ok, tried something different,

Now I'm getting -

java.lang.InstantiationException: Author
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement ArrayList.add(Author);
Continuing ...
 
Ok, tried something different,

Now I'm getting -

java.lang.InstantiationException: Author
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement ArrayList.add(Author);
Continuing ...
This looks like it's more to do with your model than the serialization code. Can you post the full model, perhaps use something like pastebin to keep the forum a bit cleaner.
 
Back
Top Bottom