今天恰好聽一位長輩訴說XML的一些優點,可交換性、檔案小等,於是乎就上java tutorial看了簡單的範例,順便把目前在手上處理的資料拿來練習,有JAXB API進行java<->xml真的不難,宣告JAXBContext(主要的處理mapping抽象類別)並傳入欲轉換的POJO類別,要進行java-to-xml則是使用Marshaller類別,建立實例後呼叫marshal並傳入pojo以及outputstream,XML就產生了,相反的,進行xml-to-java則是使用Unmarshaller類別,或許這是簡單的例子,更進階JAXB annotations應用可以參考官方API,以下是今天自己練習的code。
參考網址:http://docs.oracle.com/javase/tutorial/jaxb/intro/index.html
XMLProcess
Main
package xml;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import read.GetDataFromFile;
import alarm.Alarm;
public class XMLProcess {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Path path=Paths.get("L:\\alarm.xml");//取得檔案路徑
OutputStream out=Files.newOutputStream(path, StandardOpenOption.WRITE);//建立output物件
JAXBContext alcon;//主要處理java/xml mapping的抽象類別
try{
alcon=JAXBContext.newInstance(Alarm.class);//建立Alarm連結,另外要注意Alarm類別要加註@XMLRootElement,
//否則會拋出找不到rootelement例外
Marshaller m = alcon.createMarshaller();//從java object轉換到xml是使用Marshaller類別
//反向則是用Unmarshaller
GetDataFromFile gf=new GetDataFromFile();
List<Alarm> aplist=gf.readFromExcel();//一大堆Alarm物件
Iterator<Alarm> it=aplist.iterator();
while(it.hasNext()){
m.marshal(it.next(), out);//將物件轉換成XML格事後寫入檔案
}
}catch (Exception e){
e.printStackTrace();
}
}
}
POJO
package alarm;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Alarm {
private String refid;
private String alarmid;
private String des;
private String state;
private String sys;
public String getRefid() {
return refid;
}
public void setRefid(String refid) {
this.refid = refid;
}
public String getAlarmid() {
return alarmid;
}
public void setAlarmid(String alarmid) {
this.alarmid = alarmid;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getSys() {
return sys;
}
public void setSys(String sys) {
this.sys = sys;
}
}
output:(沒有特別format,是很ugly)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><alarm><alarmid>A0G42:LT0001_SA.PNT</alarmid><des>SKIMMER SURGE TANK TNK-0001B LVL</des><refid>0G421003A003</refid><state></state><sys>0G42</sys></alarm><?xml version="1.0" encoding="UTF-8" standalone="yes"?><alarm><alarmid>A0G42:LT0001_SA.HIABS</alarmid><des>SKIMMER SURGE TANK TNK-0001B LVL</des><refid>0G421003A003</refid><state>HI HI</state><sys>0G42</sys></alarm><?xml version="1.0" encoding="UTF-8" standalone="yes"?><alarm><alarmid>A0G42:LT0001_SA.LOABS</alarmid><des>SKIMMER SURGE TANK TNK-0001B LVL</des><refid>0G421003A003</refid><state>LOW</state><sys>0G42</sys></alarm><?xml version="1.0" encoding="UTF-8" standalone="yes"?><alarm><alarmid>A0G42:LT0001_SA.LLOABS</alarmid><des>SKIMMER SURGE TANK TNK-0001B LVL</des><refid>0G421003A003</refid><state>LOW LOW</state><sys>0G42</sys></alarm><?xml version="1.0" encoding="UTF-8" standalone="yes"?><alarm><alarmid>A0G42:LT0010_SA.PNT</alarmid><des>AFPC POOL LEVEL</des><refid>0G421003A013</refid><state></state><sys>0G42</sys></alarm>