參照網上這篇博客所寫,在此表示感謝java
http://blog.csdn.net/manymore13/article/details/8565989 dom
package com.qiux.demo; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class CardsJava2XML { public final static int cardlength = 18; public final static String PREFIX_IMSI = "9000000000"; public final static String PREFIX_ICCID = "90000000000000"; public final static String PREFIX_MSISDN = "90000"; public final static String PREFIX_COUNTER = "1"; public final static String key_auth_text = "45384237354543363731464333364143"; public final static String key_cipher_text = "45384237354543363731464333364143"; public final static String key_portalkey_text = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"; public class Card{ ArrayList<Key> keylist ; String imsi; String iccid; String msisdn; String counter; public Card( ArrayList<Key> keylist){ this.keylist = keylist; } public Card(String imsi, String iccid, String msisdn, String counter){ this.imsi = imsi; this.iccid = iccid; this.msisdn = msisdn; this.counter = counter; } public Card( ArrayList<Key> keylist , String imsi , String iccid , String msisdn , String counter ){ this.keylist = keylist; this.imsi = imsi; this.iccid = iccid; this.msisdn = msisdn; this.counter = counter; } public ArrayList<Key> getKeylist() { return keylist; } public void setKeylist(ArrayList<Key> keylist) { this.keylist = keylist; } public String getImsi() { return imsi; } public void setImsi(String imsi) { this.imsi = imsi; } public String getIccid() { return iccid; } public void setIccid(String iccid) { this.iccid = iccid; } public String getMsisdn() { return msisdn; } public void setMsisdn(String msisdn) { this.msisdn = msisdn; } public String getCounter() { return counter; } public void setCounter(String counter) { this.counter = counter; } public int getsize(){ if(keylist != null){ return keylist.size(); } return 0; } } public class Key{ String type; String index; public Key(String type, String index){ this.index = index; this.type = type; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getIndex() { return index; } public void setIndex(String index) { this.index = index; } } public void BuildXMLDoc() throws IOException, JDOMException { // 建立根節點 並設置它的屬性 ; Element root = new Element("Cards"); // 將根節點添加到文檔中; Document Doc = new Document(root); ArrayList<Key> listkey3 = new ArrayList<Key>(); Key key1 = new Key("cipher","1"); Key key2 = new Key("auth","1"); Key key3 = new Key("portalkey","1"); listkey3.add(key1); listkey3.add(key2); listkey3.add(key3); for(int i = 0; i < cardlength ; i++){ Element elements = new Element("Card"); elements.setAttribute("IMSI",PREFIX_IMSI+String.format("%05d", i+1)); elements.setAttribute("ICCID",PREFIX_ICCID+String.format("%05d", i+1)); elements.setAttribute("MSISDN",PREFIX_MSISDN+String.format("%05d", i+1)); elements.setAttribute("COUNTER",PREFIX_COUNTER); for(int j=0; j< 3 ; j++){ Element keyelement = new Element("Key"); keyelement.setAttribute("type",listkey3.get(j).getType()); keyelement.setAttribute("index",listkey3.get(j).getIndex()); if("portalkey".equals(listkey3.get(j).getType())){ elements.addContent(keyelement.setText(key_portalkey_text)); } if("auth".equals(listkey3.get(j).getType())){ elements.addContent(keyelement.setText(key_auth_text)); } if("cipher".equals(listkey3.get(j).getType())){ elements.addContent(keyelement.setText(key_cipher_text)); } } // root.addContent(elements); } // 輸出 books.xml 文件; // 使xml文件 縮進效果 Format format = Format.getPrettyFormat(); XMLOutputter XMLOut = new XMLOutputter(format); XMLOut.output(Doc, new FileOutputStream("c:/cards.xml")); } public static void main(String[] args) { try { CardsJava2XML j2x = new CardsJava2XML(); System.out.println("正在生成 cards.xml 文件..."); j2x.BuildXMLDoc(); } catch (Exception e) { e.printStackTrace(); } System.out.println("c:/cards.xml 文件已生成"); } }