因为项目中使用了Castor(xml和对象间的转换),所以自己在网上找了一些资料练习了一下,现在把代码贴出来,留着以后看看!
这是用到的jar包,有一些是没用的!这都是在网上看的
一、(没有配置文件)
1 public class DefaultCastor 2 { 3 //返回此抽象路径的绝对路径字符串 4 public static final String xmlPath=new File("").getAbsolutePath()+"\src\CastorTest\"; 5 6 @org.junit.Test 7 public void Test(){ 8 // DefaultCastor test=new DefaultCastor(); 9 try 10 { 11 beanToxml(); 12 xmlTobean(); 13 } catch (MarshalException e) 14 { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } catch (ValidationException e) 18 { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } catch (IOException e) 22 { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 } 27 /** 28 * 缺省用法没有配置文件 29 * 编组(marshalling) 30 * @throws IOException 31 * @throws ValidationException 32 * @throws MarshalException 33 */ 34 @SuppressWarnings({ "unchecked", "rawtypes" }) 35 public static void beanToxml() throws IOException, MarshalException, ValidationException{ 36 System.out.println(xmlPath+"路径------"); 37 List friend=new ArrayList(); 38 friend.add("zhangsan"); 39 friend.add("lisi"); 40 Map score=new HashMap(); 41 score.put("1", "za"); 42 score.put("2", "na"); 43 String[] hobby={"a","b"}; 44 UserBean user=new UserBean(); 45 user.setName("fenghao"); 46 user.setAge("24"); 47 user.setFriends(friend); 48 user.setHobby(hobby); 49 user.setScore(score); 50 File file=new File(xmlPath+"test.xml"); 51 Writer writer=new FileWriter(file); 52 Marshaller shaller=new Marshaller(writer); 53 shaller.setEncoding("utf-8"); 54 shaller.marshal(user); 55 writer.flush(); 56 writer.close(); 57 } 58 /** 59 * xml转成Bean 60 * 解组 61 * @throws FileNotFoundException 62 * @throws ValidationException 63 * @throws MarshalException 64 */ 65 public static void xmlTobean() throws FileNotFoundException, MarshalException, ValidationException{ 66 File file=new File(xmlPath+"test.xml"); 67 System.out.println(file.getAbsolutePath()+"文件路径"); 68 Reader read=new FileReader(file); 69 // InputStream in=new FileInputStream(file); 70 UserBean user=(UserBean)Unmarshaller.unmarshal(UserBean.class, read); 71 System.out.println("--------------/n"); 72 System.out.println(user.getName()+"-------name/n"+user.getScore().get("1")); 73 System.out.println("---------------"); 74 try 75 { 76 read.close(); 77 } catch (IOException e) 78 { 79 // TODO Auto-generated catch block 80 e.printStackTrace(); 81 } 82 83 } 84 85 }
二、
这是实体类
1 package CastorTest; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Map; 6 7 public class UserBean 8 { 9 private String name; 10 private String age; 11 private String[] hobby; 12 private Map score; 13 private List<String> friends; 14 public String getName() 15 { 16 return name; 17 } 18 public void setName(String name) 19 { 20 this.name = name; 21 } 22 public String getAge() 23 { 24 return age; 25 } 26 public void setAge(String age) 27 { 28 this.age = age; 29 } 30 public String[] getHobby() 31 { 32 return hobby; 33 } 34 public void setHobby(String[] hobby) 35 { 36 this.hobby = hobby; 37 } 38 public Map getScore() 39 { 40 return score; 41 } 42 public void setScore(Map score) 43 { 44 this.score = score; 45 } 46 public List<String> getFriends() 47 { 48 return friends; 49 } 50 public void setFriends(List<String> friends) 51 { 52 this.friends = friends; 53 } 54 public UserBean(String name, String age, String[] hobby, Map score, 55 List<String> friends) 56 { 57 super(); 58 this.name = name; 59 this.age = age; 60 this.hobby = hobby; 61 this.score = score; 62 this.friends = new ArrayList<String>(); 63 } 64 public UserBean() 65 { 66 super(); 67 // TODO Auto-generated constructor stub 68 } 69 70 71 }
1 package CastorTest; 2 //这是工具类 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 11 12 public class FileUtil 13 { 14 public static String readFile(String filePath) throws IOException{ 15 File file=new File(filePath); 16 BufferedReader read=new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8")); 17 StringBuffer str=new StringBuffer((int)file.length()+300);//细节性优化方案 18 String line=null; 19 while((line=read.readLine())!=null){ 20 str.append(line); 21 } 22 read.close(); 23 return str.toString(); 24 } 25 public static String readFile1(String filePath) throws IOException{ 26 File file=new File(filePath); 27 InputStream in=null; 28 in=new FileInputStream(file); 29 byte b[]=new byte[(int)file.length()];//细节性优化方案 30 int len=in.read(b); 31 in.close(); 32 String str=new String(b,0,len); 33 return str; 34 } 35 }
这是映射文件(描述xml和Bean的关系)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" "http://castor.org/mapping.dtd">
<mapping>
<class name="CastorTest.UserBean" auto-complete="true">
<map-to xml="dataset"/>
<field name="name" type="java.lang.String">
<bind-xml name="name" node="element"/>
</field>
<field name="age" type="java.lang.String">
<bind-xml name="age" node="element"/>
</field>
<field name="hobby" type="java.lang.String" collection="array">
<bind-xml name="hobby" node="element"/>
</field>
<field name="score" collection="map">
<class name="org.exolab.castor.mapping.MapItem">
<field name="key" type="java.lang.String">
<bind-xml name="key" node="attribute"/>
</field>
<field name="value" type="java.lang.String">
<bind-xml name="value" node="text"/>
</field>
</class>
</field>
<field name="friends" collection="arraylist" type="java.lang.String">
<bind-xml name="friends" node="element"/>
</field>
</class>
</mapping>
这是运行代码:
1 package CastorTest; 2 3 4 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.FileReader; 8 import java.io.FileWriter; 9 import java.io.IOException; 10 import java.io.Reader; 11 import java.io.StringReader; 12 import java.io.Writer; 13 import java.util.ArrayList; 14 import java.util.HashMap; 15 import java.util.List; 16 import java.util.Map; 17 18 import org.exolab.castor.mapping.Mapping; 19 import org.exolab.castor.mapping.MappingException; 20 import org.exolab.castor.xml.MarshalException; 21 import org.exolab.castor.xml.Marshaller; 22 import org.exolab.castor.xml.Unmarshaller; 23 import org.exolab.castor.xml.ValidationException; 24 /** 25 * 使用配置文件进行javaBean和xml的转化 26 * @author issuser 27 * 28 */ 29 public class CastorUtil 30 { 31 private static final String filePath=new File("").getAbsolutePath()+"\src\CastorTest\"; 32 private static Mapping loadMapping(){ 33 Mapping mapping=new Mapping(); 34 try 35 { 36 mapping.loadMapping(filePath+"Mapping.xml"); 37 } catch (IOException e) 38 { 39 // TODO Auto-generated catch block 40 e.printStackTrace(); 41 } catch (MappingException e) 42 { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 } 46 return mapping; 47 } 48 @org.junit.Test 49 public void Test(){ 50 CastorUtil test=new CastorUtil(); 51 try 52 { 53 // test.beanToXml(); 54 test.readXmlToBean(); 55 } catch (MarshalException e) 56 { 57 // TODO Auto-generated catch block 58 e.printStackTrace(); 59 } catch (ValidationException e) 60 { 61 // TODO Auto-generated catch block 62 e.printStackTrace(); 63 } catch (IOException e) 64 { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } catch (MappingException e) 68 { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 } 72 } 73 @SuppressWarnings("unchecked") 74 public void beanToXml() throws IOException, MarshalException, ValidationException, MappingException{ 75 List friend=new ArrayList(); 76 friend.add("zhangsan"); 77 friend.add("lisi"); 78 Map score=new HashMap(); 79 score.put("1", "za"); 80 score.put("2", "na"); 81 String[] hobby={"a","b"}; 82 UserBean user=new UserBean(); 83 user.setName("fenghao"); 84 user.setAge("24"); 85 user.setFriends(friend); 86 user.setHobby(hobby); 87 user.setScore(score); 88 89 File file=new File(filePath+"test.xml"); 90 Writer writer=new FileWriter(file); 91 Marshaller shaller=new Marshaller(writer); 92 shaller.setEncoding("utf-8"); 93 shaller.setMapping(CastorUtil.loadMapping()); 94 shaller.marshal(user); 95 writer.flush(); 96 writer.close(); 97 98 } 99 public void readXmlToBean() throws MappingException, IOException, MarshalException, ValidationException{ 100 String xmlString=FileUtil.readFile1(filePath+"test.xml"); 101 Reader read=new StringReader(xmlString); 102 Unmarshaller unshaller=new Unmarshaller(UserBean.class); 103 unshaller.setMapping(CastorUtil.loadMapping()); 104 UserBean user=(UserBean)unshaller.unmarshal(read); 105 System.out.println(user.getName()+"--name--"+user.getAge()+"--age--"); 106 read.close(); 107 } 108 }