Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 9:38 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 3 posts ] 
Author Code Snippet
 Code subject: Marshal Java object to XML file
PostPosted: Tue Jul 07, 2009 11:47 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time

Marshal Java object to xml file :

In the following example , we marshal the java object to XML and write it in a file .

Code:

import java
.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;


public class 
ObjectToXml {
  public static 
void main(String[] argsthrows Exception {
    
JAXBContext contextObj JAXBContext.newInstance(Employee.class);

    
Marshaller marshallerObj contextObj.createMarshaller();
    
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUTtrue);

    
Student myStudent = new Student();
    
myStudent.setGender("M");
    
myStudent.setName("Amar");
    
myStudent.setAge(20);
    
marshallerObj .marshal(object, new FileOutputStream("Student.xml"));  
 

  }


Code:

import javax
.xml.bind.annotation.XmlRootElement;

@
XmlRootElement
class Student{
  private 
String gender;

  private 
String name;

  private 
int age;

  public 
String getGender() {
    return 
gender;
  }

  public 
void setGender(String gender) {
    
this.gendergender;
  }

  public 
String getName() {
    return 
name;
  }

  public 
void setName(String name) {
    
this.name name;
  }

  public 
int getAge() {
    return 
age;
  }

  public 
void setAge(int age) {
    
this.ageage;
  }


Output XML file example :

Code:
 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
    <gender>M</gender>
    <name>Amar</name>
    <age>20</age>
</student>

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: Marshal Java object to XML file
PostPosted: Fri Jul 10, 2009 9:19 am 
Offline
Newbie
User avatar

Joined: Fri Jul 10, 2009 9:17 am
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
Today I had a little problem with component mapping and the attributes. And It has something to do with the fact that all my objects use interfaces.


TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: Marshal Java object to XML file
PostPosted: Sun Jul 12, 2009 8:08 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
how ?

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 3 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 Java seminar topic with demo  Java  Anonymous  1
 java project code  Java  Anonymous  0
 change Font of text in java  Java examples  msi_333  1
 Read your gmail using Java code  Java examples  msi_333  5
 project source code in java  Java  Anonymous  0

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team