I would like to know why my unmarshalling process cause some troubles:
- I save my java object in a xml file.
- I load my java object from the xml file
Once that's done, strange behaviour occurs in the method of my java object (ClassMain.java).
Indeed the method isLogin()
returns false just before it returns true (ClassMain.java. Any ideas?
MainClass
public static void main(String[] args) {
Player p1 = new Player();
p1.setLogin("p1");
p1.setMdp("mdp1");
try {
//Test : verify that player's login is 'p1' (return true)
System.out.println(p1.isLogin("p1"));
marshaling(p1);
Player pfinal =unMarshaling();
//Test : verify that player's login is 'p1' (return False ?why?)
System.out.println(pfinal.isLogin("p1"));
} catch (JAXBException e) {
e.printStackTrace();
}
}
private static Player unMarshaling() throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Player.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Player player = (Player) jaxbUnmarshaller.unmarshal( new File("C:/Users/Public/player.xml") );
return player;
}
private static void marshaling(Object o) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Player.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(o, new File("C:/Users/Public/player.xml"));
}}
Player Class
@XmlRootElement(name = "joueur")
@XmlAccessorType (XmlAccessType.FIELD)
public class Player{
@XmlAttribute
private String login;
public Player() {
}
public String getLogin() {
return this.login;
}
public void setLogin(String login) {
this.login = login;
}
public boolean isLogin(String n){
if(this.login == n)
return true;
else
return false;
}
}