Object Xml Encoder using Java Bean XML Encoder - Java XML

Java examples for XML:XML Java Bean

Description

Object Xml Encoder using Java Bean XML Encoder

Demo Code


//package com.java2s;

import java.beans.XMLEncoder;
import java.io.File;

import java.io.FileOutputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object obj = "java2s.com";
        String fileName = "java2s.com";
        ObjectXmlEncoder(obj, fileName);
    }//w w w.j  ava2s .  c o m

    public static void ObjectXmlEncoder(Object obj, String fileName)

    {
        // 
        File fo = new File(fileName);
        // 
        if (!fo.exists()) {
            // 
            String path = fileName.substring(0, fileName.lastIndexOf('.'));
            File pFile = new File(path);
            pFile.mkdirs();
        }
        try {
            //          
            FileOutputStream fos = new FileOutputStream(fo);
            // XML      
            XMLEncoder encoder = new XMLEncoder(fos);
            // XML
            encoder.writeObject(obj);
            encoder.flush();
            //          encoder.close();
            //          fos.close();
        } catch (Exception e) {
            // TODO: handle exception
        }

    }
}

Related Tutorials