Results 1 to 7 of 7
Thread: XMLEncoder e XMLDecoder
- 09-26-2011, 02:08 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
XMLEncoder e XMLDecoder
Hi!
I have this problem...
java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class Person with modifiers ""
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Person);
Continuing ...
My code
Person class
public class Person implements Serializable
{
private String name = null;
private String addr = null;
Person(){}
Person(String n, String a)
{
name = n;
addr = a;
}
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void setAddr(String a)
{
addr = a;
}
public String getAddr()
{
return addr;
}
public String toString()
{
return("Name: "+getName()+"\n "+"Address: "+getAddr()+");
}
}
Main class
try{
FileOutputStream os = new FileOutputStream("person.xml");
XMLEncoder encoder = new XMLEncoder(os);
Person d = new Person();
d.setName("Sara");
d.setAddr("London");
encoder.writeObject(d);
encoder.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Can you help me please! :)
Thanks
Paula
- 09-26-2011, 02:55 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: XMLEncoder e XMLDecoder
Your Person constructor has default access privileges...that is it is not marked as private/protected or public. This means it is only accessible by classes declared in the same package as it (which looks like the default package). The XMLEncoder seems to need access to these, but it can't access them because it isn't part of that package.
- 09-26-2011, 03:01 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: XMLEncoder e XMLDecoder
Thanks for your help!
That was easy it makes sense! :D
- 09-26-2011, 03:28 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: XMLEncoder e XMLDecoder
Just one more thing...I promisse I don´t bother you anymore! :D
The code above was just a test. I want to write an arrayList...so I did this...
try{
Person d = new Person();
FileOutputStream os = new FileOutputStream("person.xml");
XMLEncoder encoder = new XMLEncoder(os);
for (int x=0; x<(Datastore.getSingletonObject().listPerson.getAr rayListSize()); x++)
{
d.setName(Datastore.getSingletonObject().listPerso n.getPerson(x).getName());
d.setAddress(Datastore.getSingletonObject().listPe rson.getPerson(x).getAddress());
System.out.println(d);
encoder.writeObject(d);
}
encoder.close();
The println its ok! Prints the arrayList, but the XML file shows just the first person in the arrayList...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_26" class="java.beans.XMLDecoder">
<object id="Person0" class="Person">
<void property="address">
<string>London</string>
</void>
<void property="name">
<string>Anna</string>
</void>
</object>
<object idref="Person0"/>
<object idref="Person0"/>
<object idref="Person0"/>
<object idref="Person0"/>
</java>
What I'm doing wrong? Must be obvious...I know... :/
Thanks again! :)
Paula
- 09-26-2011, 04:04 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: XMLEncoder e XMLDecoder
We've seen something like this here before (I think).
It's because you;re using the same Person object, and haven't flushed.
The XMLEncoder knows it's already written d so simply sticks a reference to it (the idref ones).
You should either be creating new Person objects for each entry you want to add, or call flush after each write.
I would argue (quite a lot in fact) that you should do the former, since these are different Persons, and not simply the same one who likes to change their name a lot.
So:
(and I largely did that to show you code tags).Java Code:loop encoder.writeObject(new Person(...parameters...));
ETA: Ooooooh, code blocks have changed (or have I hit some odd button or other)?
- 09-26-2011, 04:31 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: XMLEncoder e XMLDecoder
flush worked! :D
Thank you very very much Tolls!
see ya
Paula
- 09-28-2011, 01:33 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: XMLEncoder e XMLDecoder
Here's another way...working with the arrayList :D
XMLEncoder
try{
FileOutputStream os = new FileOutputStream("person.xml");
XMLEncoder encoder = new XMLEncoder(os);
encoder.writeObject(Datastore.getSingletonObject() .listPerson);
encoder.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
XMLDecoder
try{
FileInputStream os = new FileInputStream("person.xml");
XMLDecoder decoder = new XMLDecoder(os);
Datastore.getSingletonObject().listPerson=(ListPer son)decoder.readObject();
decoder.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
PS1 - The class ListPerson must have a a constructor with no arguments and getters and setters...
PS2 - The calsses ListPerson and Person must implements Serializable
Similar Threads
-
why XMLEncoder is not encoding all public properties?
By getstarted in forum XMLReplies: 1Last Post: 01-10-2009, 02:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks