castor框架使用

xml字符串转成JAVA对象

字符串中有中文问题造成xml解析不完整
下面给出转换代码:(其它配置详见castor doc)
public static Object xmlToObject(String xml,String mappingFile){
         Mapping map = new Mapping();
         Object object=null;
         try {
             map.loadMapping(CastorUtil.class.getResource(mappingFile));
             Unmarshaller unmar = new Unmarshaller(map);
             object = unmar.unmarshal(new InputSource(new StringReader(xml)) );
             Marshaller marshaller = new Marshaller(new OutputStreamWriter(
                     System.out));
             marshaller.setMapping(map);
             marshaller.marshal(object);
           
         } catch (Exception e) {
             e.printStackTrace();
         }
       
         return object;
    }

你可能感兴趣的:(框架,xml)