Tech-Today
How to create an xsd and xml data from model entity in java
The following code will show how to generate a xsd base on a given model and a sample xml with data.
Let's assume you have a CustomerList model, which is an array of Customer.
JAXBContext context;
try {
context = JAXBContext.newInstance(CustomerList.class);
context.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String namespaceURI,
String suggestedFileName) throws IOException {
File file = new File("c://temp//customer.xsd");
StreamResult result = new StreamResult(file);
result.setSystemId(file.toURI().toURL().toString());
return result;
}
});
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(listToExport, new FileOutputStream(
"c://temp//customers.xml"));
} catch (JAXBException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Next is how are we going to read a java object from xml file. Let the code explain :-)
JAXBContext jaxbContext = JAXBContext.newInstance(CustomerList.class);
InputStream is = getClass().getClassLoader().getResourceAsStream(
"customer.xml");
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
CustomerListmessages = (CustomerList) jaxbUnmarshaller.unmarshal(is);
Useful tool in transforming an xml to xsd:
http://www.freeformatter.com/xsd-generator.html#ad-output
-
How To Encrypt And Decrypt An Object In And From A File In Java
There are times when we need to write something on a file, but doesn't want it to be readable as a plain text. In this case we can use any type of encryption mechanism, but what if we want to decrypt the encrypted file back and read its contents....
-
How To Download A File From The Server Using Javaee
The following snippet will accept a filename in the server's directory, set it as the content of the FacesContext for the user to download. public String downloadXMLInvoice(String fileName) { File file = new File(getXmlInvoiceDir().getAbsolutePath()...
-
How To Encode And Decode A Json Encrypted String In Java
This tutorial requires the use of apache commons and jackson libraries. For example I have a highly confidential string and I want to send it over the net, of course normally I want to encrypt it. And one of the method we can send this encrypted string...
-
How To Load Property File From Glassfish Config's Folder
In seam you can define a component class in components.xml that will load the properties from the JBOSS_CONFIG folder. Add the ff lines: <component name="paramBean" class="com.ipil.PropertyBean" scope="application" auto-create="true" startup="true">...
-
Mvc3 Dynamic Search Paging Using Pagedlist
Basically there's a great tutorial from this site: Unboxed solutions. -I've copied the codes and add some comments. I'll just try to add my own comments. 1.) First you need to install PagedList.Mvc from Nuget. a.) Go to Tools->Library Package...
Tech-Today