星期四, 7月 06, 2006

JDOM(generate xml)備忘記

續上一編讀取Excel後,
最後還需要將資料轉成為xml,發佈到其他地方或資料庫.

下載jdom-1.0.zip
http://www.jdom.org/dist/binary/

解壓後將jdom.jar加入到classpath中.

Account.java
public class Account{
private String name;
private String password;
private String email;
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){
this.password= password;
}
public String getEmail(){
return this.email;
}
public void setAge(String email){
this.email= email;
}
public String getClassName() {
return this.getClass().getName().substring(
this.getClass().getPackage().getName().length() + 1);
}

public String toString(){
return getClassName() + "@" this.name + "#" + this.password + "#" + this.email;
}

}

GenerateXML.java
public class GenerateXML(){
private String xmlFilePath = "account.xml";
public void addNode(Element accountElement,String nodeName,String nodeData){
Element tmpElement = new Element(nodeName); // 建立新element
tmpElement.addContent(nodeData); // 將content加入到element裡
accountElement.addContent(tmpElement); // 將有content的element加入到預設的element
}

public void generateXML() throws IOException{
Element rootElement = new Element("ACCOUNTS"); // 建立 Root Element
Document document = new Document(rootElement); // 建立 Xml Document
Element accountElement = null; // 設定Element, 這個Element將會加入到 Root Element裡

accountElement = new Element("ACCOUNT"); // 建立新的Element
addNode(accountElement,"NAME", "joeyta"); // 將name element加入到root element裡
addNode(accountElement,"PASSWORD", "123456");
addNode(accountElement,"EMAIL", " joeyta@matrix.org.cn");
rootElement.add(accountElement);

accountElement = new Element("ACCOUNT");
addNode(accountElement,"NAME", "jane");
addNode(accountElement,"PASSWORD", "123123");
addNode(accountElement,"EMAIL", "jane@matrix.org.cn");
rootElement.add(accountElement);

Format format = Format.getCompactFormat(); // 建立輸出格式物件
format.setEncoding("big5"); // 設定輸出encoding 為big5
format.setOmitEncoding(false); // 設定是否省略輸出的encoding
format.setIndent(" "); // 設定縮排時的格式
format.setLineSeparator(System.getProperty("line.separator")); // 設定隔行的格式


XMLOutputter outputter = new XMLOutputter(); // 建主輸出物件
outputter.setFormat(format); // 加入輸出格式物件

System.out.println(outputter.outputString(document)); // 打印xml出來

FileWriter fw = new FileWriter(xmlFilePath); // 建立writer
BufferedWriter fout = new BufferedWriter(fw);
outputter.output(document,fw); // 產生xml檔案
fout.close();
}

public static void main(String args){
GenerateXML gx = new GenerateXML();
gx.generateXML();

}
}

輸出的account.xml內容:
<?xml version="1.0" encoding="big5"?>
<ACCOUNTS>
<ACCOUNT>
<NAME>joeyta</NAME>
<PASSWORD>123456</PASSWORD>
<EMAIL>joeyta@matrix.org.cn</EMAIL>
</ACCOUNT>
<ACCOUNT>
<NAME>jane</NAME>
<PASSWORD>123123</PASSWORD>
<EMAIL>jane@matrix.org.cn</EMAIL>
</ACCOUNT>
</ACCOUNTS>

以下有更多官方的文檔及教學:
http://www.jdom.org/downloads/docs.html
http://www.cafeconleche.org/books/xmljava/

沒有留言: