Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Unmarshalling


Code ResourceAuthor: morla  

Difficulty Level: Beginner

Published: 7th Sep 2010   Read: 2852 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 
This is code for unmarshal a file into a stream
 


Sample Code
  1. /*
  2.  * To change this template, choose Tools Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package com.nag.form
  7.  
  8. import com.bind.Expences
  9. import java.io.BufferedWriter
  10. import java.io.File
  11. import java.io.FileNotFoundException
  12. import java.io.FileOutputStream
  13. import java.io.IOException
  14. import java.io.OutputStreamWriter
  15. import java.util.logging.Level
  16. import java.util.logging.Logger
  17.  
  18. /**
  19.  *
  20.  * @author nageswararao.m
  21.  */
  22. public class Marshaller {
  23.     public void doMarshalling(Expences sb){
  24.         try {
  25.             javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(sb.getClass().getPackage().getName())
  26.             javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller()
  27.             marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8") //NOI18N
  28.             marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE)
  29.             try {
  30.                 File f=new File("exp.xml")
  31.                 if(!f.isFile()){
  32.                     try {
  33.                         f.createNewFile()
  34.                         System.out.println("file "+f.getAbsolutePath())
  35.                         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)))
  36.                     bw.write("<?xml version="1.0" encoding="UTF-8" standalone="yes"?>" + 'n' + "<expences/>")
  37.                     bw.flush()
  38.                     } catch (IOException ex) {
  39.                         Logger.getLogger(Marshaller.class.getName()).log(Level.SEVERE, null, ex)
  40.                     }
  41.                 }
  42.                 marshaller.marshal(sb, new FileOutputStream(f))
  43.             } catch (FileNotFoundException ex) {
  44.                 Logger.getLogger(Marshaller.class.getName()).log(Level.SEVERE, null, ex)
  45.             }
  46.         } catch (javax.xml.bind.JAXBException ex) {
  47.             // XXXTODO Handle exception
  48.             java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex) //NOI18N
  49.         }
  50.     }
  51. public void doMarshalling(com.bind.getmoney.PaidMoney sb){
  52.         try {
  53.             javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(sb.getClass().getPackage().getName())
  54.             javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller()
  55.             marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8") //NOI18N
  56.             marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE)
  57.             try {
  58.                 File f=new File("income.xml")
  59.                 if(!f.isFile()){
  60.                     try {
  61.                         f.createNewFile()
  62.                         System.out.println("file "+f.getAbsolutePath())
  63.                         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)))
  64.                     bw.write("<?xml version="1.0" encoding="UTF-8" standalone="yes"?>" + 'n' + "<PaidMoney/>")
  65.                     bw.flush()
  66.                     } catch (IOException ex) {
  67.                         Logger.getLogger(Marshaller.class.getName()).log(Level.SEVERE, null, ex)
  68.                     }
  69.                 }
  70.                 marshaller.marshal(sb, new FileOutputStream(f))
  71.             } catch (FileNotFoundException ex) {
  72.                 Logger.getLogger(Marshaller.class.getName()).log(Level.SEVERE, null, ex)
  73.             }
  74.         } catch (javax.xml.bind.JAXBException ex) {
  75.             // XXXTODO Handle exception
  76.             java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex) //NOI18N
  77.         }
  78.     }
  79.  
  80. }
  81.  
Copyright GeekInterview.com


Next Article: Find Number of Files and Sub Directories


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora140723
2. iamdvr102875
3. venki_madesh35424
4. Raju20169
5. parmod kumar duhan16408
6. Kiran.jakkaraju15125
7. chowsys7787
8. ashish.cns6882
9. Venkateswara Rao6774
10. venkat_kiran5893

Active Coders

Refined Tags