11/17/2009

Object to XML by DOM API in Java

有時侯我們想要把java object轉成XML時會使用到的code,轉出來的xml約像下面的格式。

   A12345
   1
   B12345
   2
   Iden123
   李昭慶
   
    d1
    c1
    1
    1
    1
   
   
    d2
    c2
    2
    2
    2
   
   
    d3
    c3
    3
    3
    3
   
  
DOMtoXML.java
/**
 * Object output XML format by DOM.
 */
package com.iden.Dom;

import java.io.*;
import java.util.ArrayList;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DOMtoXML {
   
 DOMtoXML(){}
 
 public static void main(String[] args) {  
  DOMtoXML jt = new DOMtoXML();
    
  Lot lot = new Lot("A12345", "1", "B12345", "2", "Iden123", "李昭慶");
  lot.putGlass("d1", "c1", "1", "1", "1");
  lot.putGlass("d2", "c2", "2", "2", "2");
  lot.putGlass("d3", "c3", "3", "3", "3");  
  String xml = lot.genObjectToXML();  
  System.out.println(xml); //print the xml string
 }
  
 private String encoding(String s){     
     try {      
   return new String(s.getBytes(), "UTF-8"); //"ISO-8859-1"
  } catch (UnsupportedEncodingException e) {   
   e.printStackTrace();
  }
  return null;
    }  
}

class Lot {
 DocumentBuilderFactory f;
 DocumentBuilder b;
 Document doc;
 private String lotId1;
 private String glsQty1;
 private String lotId2;
 private String glsQty2;
 private String userId;
 private String userName;
 private ArrayList glasses;
 
 Lot(String lotId1, String glsQty1, String lotId2, String glsQty2, String userId, String userName) { 
  this.glasses = new ArrayList();
  this.lotId1 = lotId1;
  this.glsQty1 = glsQty1;
  this.lotId2 = lotId2;
  this.glsQty2 = glsQty2;
  this.userId = userId;
  this.userName = userName;
    
  f = DocumentBuilderFactory.newInstance();
  try {
   b = f.newDocumentBuilder();
  } catch (ParserConfigurationException e) {   
   e.printStackTrace();
  }
  doc = b.newDocument();
  
 }

 public String genObjectToXML(){  
  try {   
   /*
    * XML output format:    
    */   
   Element root = doc.createElement(this.getClass().getSimpleName()); 
   doc.appendChild(root);
         Element e;
         e = doc.createElement("lotId1");
         root.appendChild(e);
         e.appendChild(doc.createTextNode(this.lotId1));
         e = doc.createElement("glsQty1");
         root.appendChild(e);
         e.appendChild(doc.createTextNode(this.glsQty1));
         e = doc.createElement("lotId2");
         root.appendChild(e);
         e.appendChild(doc.createTextNode(this.lotId2));
         e = doc.createElement("glsQty2");
         root.appendChild(e);
         e.appendChild(doc.createTextNode(this.glsQty2));
         e = doc.createElement("userId");
         root.appendChild(e);
         e.appendChild(doc.createTextNode(this.userId));
         e = doc.createElement("userName");
         root.appendChild(e);
         e.appendChild(doc.createTextNode(this.userName)); 
         
         Element g, dpLot, cutLot, gId, location, orgLocation;
         for(int i=0;i<glasses.size();i++){
          Glass objG = (Glass)glasses.get(i);
          g = doc.createElement("glass");
          root.appendChild(g);
          
          dpLot = doc.createElement("dpLotId");
          g.appendChild(dpLot);
          dpLot.appendChild(doc.createTextNode(objG.getDpLotId()));
          cutLot = doc.createElement("cutLotId");
          g.appendChild(cutLot);
          cutLot.appendChild(doc.createTextNode(objG.getCutLotId()));
          gId = doc.createElement("glsId");
          g.appendChild(gId);
          gId.appendChild(doc.createTextNode(objG.getGlsId()));
          location = doc.createElement("location");
          g.appendChild(location);
          location.appendChild(doc.createTextNode(objG.getLocation()));
          orgLocation = doc.createElement("orgLocation");
          g.appendChild(orgLocation);
          orgLocation.appendChild(doc.createTextNode(objG.getOrgLocation()));
         }
                
         
         TransformerFactory tf = TransformerFactory.newInstance();
   Transformer m = tf.newTransformer();
   DOMSource source = new DOMSource(doc);
   ByteArrayOutputStream byte1 = new ByteArrayOutputStream();
   StreamResult result = new StreamResult(byte1);
   System.out.println("Encoding: " + m.getOutputProperty(OutputKeys.ENCODING));
   m.setOutputProperty(OutputKeys.ENCODING, "BIG5");
   m.transform(source, result);
   return byte1.toString();
   
  } catch (TransformerConfigurationException e) {
   e.printStackTrace();
  } catch (TransformerException e) {   
   e.printStackTrace();  
  }
  return null;
 }
 
 public void putGlass(String dpLotId, String cutLotId, String glsId, String location, String orgLocation) {
  if (dpLotId != null && cutLotId != null && glsId != null && location != null && orgLocation != null) {
   Glass g = new Glass();
   g.setDpLotId(dpLotId);
   g.setCutLotId(cutLotId);   
   g.setGlsId(glsId);
   g.setLocation(location);
   g.setOrgLocation(orgLocation);
   glasses.add(g);
  }
 }

 class Glass {
  private String dpLotId;
  private String cutLotId;
  private String glsId;
  private String location;
  private String orgLocation;

  public String getGlsId() {
   return glsId;
  }
  public void setGlsId(String glsId) {
   this.glsId = glsId;
  }
  public String getLocation() {
   return location;
  }
  public void setLocation(String location) {
   this.location = location;
  }
  public String getDpLotId() {
   return dpLotId;
  }
  public void setDpLotId(String dpLotId) {
   this.dpLotId = dpLotId;
  }
  public String getCutLotId() {
   return cutLotId;
  }
  public void setCutLotId(String cutLotId) {
   this.cutLotId = cutLotId;
  }
  public String getOrgLocation() {
   return orgLocation;
  }
  public void setOrgLocation(String orgLocation) {
   this.orgLocation = orgLocation;
  }
 } 
}

沒有留言:

張貼留言