1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
public static void main(String[] args) {
Personne p1 = findPersonByObject ("sophie", "medecin");
if(p1 != null)
p1.affiche();
}
public Personne findPersonByObject(String nom, String metier)
{
Personne person;
if(person == null)
return ;
if(personData.size() == 0 )
{
loadPersonDataFromFile(new File("personne.xml"));
}
personData.stream().forEach((personne) -> {
if(personne.getNom().equals(nom) && personne.getMetier().equals(metier))
{
person = personne;
}
});
return person;
}
public void loadPersonDataFromFile(File file) {
try {
JAXBContext context = JAXBContext
.newInstance(PersonListWrapper.class);
Unmarshaller um = context.createUnmarshaller();
// Reading XML from the file and unmarshalling.
PersonListWrapper wrapper = (PersoneListWrapper) um.unmarshal(file);
personData.clear();
personData.addAll(wrapper.getPersons());
} catch (Exception e) { // catches ANY exception
System.out.println("Could not load data");
System.out.println("Could not load data from file:\n" + file.getPath());
}
} |
Partager