Total members 11890 |It is currently Mon May 20, 2024 9:18 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Marshal Java object to xml file :

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

java code
import java.io.FileOutputStream;

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


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

    Marshaller marshallerObj = contextObj.createMarshaller();
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

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

  }


java 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.gender= gender;
  }

  public String getName() {
    return name;
  }

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

  public int getAge() {
    return age;
  }

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


Output XML file example :

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




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 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.



Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

how ?


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : Marshal Java object to XML file
 Marshal Java object to xml     -  
 Object File Directives Usage PIC Assembly     -  
 Read file content to StringBuffer String object     -  
 Copy file to file in java code- implementation     -  
 Java Object Reading     -  
 object's finalize() in java     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 Java File age monitoring     -  
 get the Contents of a ZIP File in java     -  
 Creating a JAR file in Java     -  



Topic Tags

Java XML, Java JAXB
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com