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>
 
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 ...
 
Back
Top Bottom