Pour récuperer le contenu d'un fichier xml On utilise JAXB on peut aussi valider notre fichier xml par un schéma XSD:
Méthode 1 :Utiliser File : vous etes supposés avoir un chemin fixe du sPathXSDFile
public static Object parser(Class cl, String sPathXMLFile, String sPathXSDFile) throws JAXBException, SAXException {
JAXBContext jc = JAXBContext.newInstance(cl);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File(sPathXSDFile));
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema(schema);
//Marshaller marshaller = jc.createMarshaller();
// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
return unmarshaller.unmarshal(new File(sPathXMLFile));
}
Pour appeller cette méthode :
Demande demandeDom = (Demande) parser(Demande.class, sPathXMLFile, sPathXSDFile);
la structure de la classe Demande est de la manière suivante:
@XmlRootElement(name="Demande")
@XmlAccessorType(XmlAccessType.FIELD)
public class Demande {
@XmlElement(name="DemandeFils")
private DemandeFils demandeFils;
// ne pas oublier get et les sets
}
Méthode2: Utiliser inputstream
public static Object parser2(Class cl, String sPathXMLFile, String sPathXSDFile) throws JAXBException, SAXException { String sPathXSDFile="/xsd/notreXsd.xsd" ; // le xsd se trouve dans le dossier xsd du projet eclipse
InputStream stream = FacesContext.getCurrentInstance().getExternalContext()
.getResourceAsStream(sPathXSDFile); Source schemaSource = new StreamSource(stream); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); JAXBContext jc= JAXBContext.newInstance(cl); Unmarshaller unmarshaller = jc.createUnmarshaller(); Schema schema = schemaFactory.newSchema(schemaSource); unmarshaller.setSchema(schema); return unmarshaller.unmarshal(new File(sPathXMLFile)); }
Méthode 1 :Utiliser File : vous etes supposés avoir un chemin fixe du sPathXSDFile
public static Object parser(Class cl, String sPathXMLFile, String sPathXSDFile) throws JAXBException, SAXException {
JAXBContext jc = JAXBContext.newInstance(cl);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File(sPathXSDFile));
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema(schema);
//Marshaller marshaller = jc.createMarshaller();
// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
return unmarshaller.unmarshal(new File(sPathXMLFile));
}
Pour appeller cette méthode :
Demande demandeDom = (Demande) parser(Demande.class, sPathXMLFile, sPathXSDFile);
la structure de la classe Demande est de la manière suivante:
@XmlRootElement(name="Demande")
@XmlAccessorType(XmlAccessType.FIELD)
public class Demande {
@XmlElement(name="DemandeFils")
private DemandeFils demandeFils;
// ne pas oublier get et les sets
}
Méthode2: Utiliser inputstream
public static Object parser2(Class cl, String sPathXMLFile, String sPathXSDFile) throws JAXBException, SAXException { String sPathXSDFile="/xsd/notreXsd.xsd" ; // le xsd se trouve dans le dossier xsd du projet eclipse
InputStream stream = FacesContext.getCurrentInstance().getExternalContext()
.getResourceAsStream(sPathXSDFile); Source schemaSource = new StreamSource(stream); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); JAXBContext jc= JAXBContext.newInstance(cl); Unmarshaller unmarshaller = jc.createUnmarshaller(); Schema schema = schemaFactory.newSchema(schemaSource); unmarshaller.setSchema(schema); return unmarshaller.unmarshal(new File(sPathXMLFile)); }
Aucun commentaire:
Enregistrer un commentaire