ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Generating an XML Document with JAXB
Subject:   JAXB 2.0 - JAXBConstructor Code
Date:   2006-03-28 20:00:11
From:   vp17in
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));
}
}


1 to 1 of 1
  1. JAXB 2.0 - JAXBConstructor Code
    2008-07-22 02:41:18  skg1 [View]

1 to 1 of 1