java xml2Object

JAXB的话是java1.5之后出现的,对于java对象和xml文件之间互相转化的操作比较方便,以下先记录下来,免得以后忘了。

要转化成xml文件的对象:

  1. package utils;  
  2.   
  3. import java.io.File;  
  4. import java.util.List;  
  5.   
  6. import javax.xml.bind.JAXB;  
  7. import javax.xml.bind.JAXBException;  
  8.   
  9. public class Test {  
  10.     private int i;  
  11.     private String s;  
  12.     private boolean b;  
  13.     private List<Integer> list;  
  14.       
  15.     public int getI() {  
  16.         return i;  
  17.     }  
  18.     public void setI(int i) {  
  19.         this.i = i;  
  20.     }  
  21.   
  22.     public String getS() {  
  23.         return s;  
  24.     }  
  25.   
  26.     public void setS(String s) {  
  27.         this.s = s;  
  28.     }  
  29.   
  30.     public boolean isB() {  
  31.         return b;  
  32.     }  
  33.   
  34.     public void setB(boolean b) {  
  35.         this.b = b;  
  36.     }  
  37.   
  38.     public List<Integer> getList() {  
  39.         return list;  
  40.     }  
  41.   
  42.     public void setList(List<Integer> list) {  
  43.         this.list = list;  
  44.     }  
  45.   
  46.         //把xml文件转化成对象   
  47.     public static Test JAXBunmarshal(File xmlFile) throws JAXBException {  
  48.         return JAXB.unmarshal(xmlFile, Test.class);  
  49.     }  
  50.   
  51.         //把对象转化成xml   
  52.     public void JAXBmarshal(File fRootDir) {  
  53.         if (!fRootDir.exists()) {  
  54.             fRootDir.mkdirs();  
  55.         }   
  56.         JAXB.marshal(thisnew File(fRootDir, this.getS() + ".xml"));  
  57.     }  
  58.       
  59. }  

主要的是JAXBunmarshal方法和JAXBmarshal方法。

测试类:

  1. package utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import javax.xml.bind.JAXBException;  
  9.   
  10. public class JAXBTest {  
  11.   
  12.     public static void main(String[] args) throws FileNotFoundException, JAXBException {  
  13.         List<Integer> testList;  
  14.         {  
  15.             Test test = new Test();  
  16.             test.setB(true);  
  17.             test.setI(5);  
  18.             test.setS("wo");  
  19.             testList = new ArrayList<Integer>();  
  20.             for (int i = 0; i < 3; i++) {  
  21.                 testList.add(i);  
  22.             }  
  23.             test.setList(testList);  
  24.             //把test类转化成xml文件,当然也可以指定成outputstream,writer,string,可以参考API   
  25.             test.JAXBmarshal(new File("c://"));  
  26.         }  
  27.         System.out.println("=================================");  
  28.         {  
  29.             //把xml文件转换成test类   
  30.             Test test = Test.JAXBunmarshal(new File("c://wo1.xml"));  
  31.             System.out.println(test.getI());  
  32.             System.out.println(test.getS());  
  33.             for (int i = 0; i < test.getList().size(); i++) {  
  34.                 System.out.println(test.getList().get(i));  
  35.             }  
  36.         }  
  37.           
  38.     }  
  39. }  

猜你喜欢

转载自guobin6125.iteye.com/blog/1533264