How To Practise An Xsd Together With Xml Information From Model Entity Inwards Java

The next code volition demo how to generate a xsd base of operations on a given model in addition to a sample xml alongside data.

Let's assume you lot accept a CustomerList model, which is an array of Customer.

JAXBContext context; seek {  context = JAXBContext.newInstance(CustomerList.class);  context.generateSchema(new SchemaOutputResolver() {   @Override   world Result createOutput(String namespaceURI,     String suggestedFileName) throws IOException {    File file = novel File("c://temp//customer.xsd");    StreamResult upshot = novel StreamResult(file);    result.setSystemId(file.toURI().toURL().toString());    provide result;   }  });  Marshaller one thousand = context.createMarshaller();  m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  m.marshal(listToExport, novel FileOutputStream(    "c://temp//customers.xml")); } grab (JAXBException e) {  e.printStackTrace(); } grab (FileNotFoundException e) {  e.printStackTrace(); } grab (IOException e) {  e.printStackTrace(); } 

Next is how are nosotros going to read a coffee object from xml file. Let the code explicate :-)
JAXBContext jaxbContext = JAXBContext.newInstance(CustomerList.class); InputStream is = getClass().getClassLoader().getResourceAsStream(   "customer.xml"); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); CustomerListmessages = (CustomerList) jaxbUnmarshaller.unmarshal(is); 

Useful tool inwards transforming an xml to xsd:
http://www.freeformatter.com/xsd-generator.html#ad-output
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.