|
Here is the code for JAXBConstructor in JAXB 2.0
package generated;
import generated.*;
import javax.xml.bind.*;
import org.w3c.dom.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class JAXBConstructor {
public void generateXMLDocument(File xmlDocument){
try {
JAXBContext jaxbContext=JAXBContext.newInstance("generated");
Marshaller marshaller=jaxbContext.createMarshaller();
generated.ObjectFactory factory=new generated.ObjectFactory();
CatalogType catalog = factory.createCatalogType();
catalog.setSection("Java Technology");
catalog.setPublisher("IBM developerWorks");
JournalType journal = factory.createJournalType();
ArticleType article = factory.createArticleType();
article.setLevel("Intermediate");
article.setDate("January-2004");
article.setTitle("Service Oriented Architecture Frameworks");
article.setAuthor("Naveen Balani");
java.util.List journalList=catalog.getJournal();
journalList.add(journal);
java.util.List articleList=journal.getArticle();
articleList.add(article);
article=factory.createArticleType();
article.setLevel("Advanced");
article.setDate("October-2003");
article.setTitle("Advance DAO Programming");
article.setAuthor("Sean Sullivan");
articleList=journal.getArticle();
articleList.add(article);
article=factory.createArticleType();
article.setLevel("Advanced");
article.setDate("May-2002");
article.setTitle("Best Practices in EJB Exception Handling");
article.setAuthor("Srikanth Shenoy");
articleList=journal.getArticle();
articleList.add(article);
JAXBElement<CatalogType> catalogElement=factory.createCatalog(catalog);
marshaller.marshal(catalogElement, new FileOutputStream(xmlDocument));
}catch (IOException e) {
System.out.println(e.toString());
}catch (JAXBException e) {
System.out.println(e.toString());
}
}
public static void main (String[] argv) {
String xmlDocument=argv[0];
JAXBConstructor jaxbConstructor=new JAXBConstructor();
jaxbConstructor.generateXMLDocument(new File(xmlDocument));
}
}
|
can u please suggest me the complete flow to marshal and unmarshal the xml file usig jaxb 2.0.
I mean i want to read some xml file and populate it with some data and then write it to some destination.
I read the a similar article but i am new to this and if u can provide me all the steps that i need to folow to do this.
It will be very helpful.