读书人

利用JAXB开展xml和javabean之间转换

发布时间: 2013-12-13 00:50:19 作者: rapoo

利用JAXB进行xml和javabean之间转换


?一些名词:


? ? ? ?2. ?生成DTD,通过一个java小工具:http://sourceforge.net/projects/xml2dtd/


? ? ? ? ? ?然后选择包等操作,一步一步就可以生成了.

File file = new File("C:/ML_OA_DATA.xml");String xmlStr = FileUtils.readFileToString(file , "UTF-8");Reader reader = new StringReader(xmlStr); Unmarshaller unmarshaller = JAXBContext.newInstance(Result.class).createUnmarshaller(); Result result = (Result) unmarshaller.unmarshal(reader); List<Result.DocInfo.Field> list = result.getDocInfo().getField();List<Attachment> eFileList = result.getAttachments().getAttachment();for (Attachment efile : eFileList) { System.out.println(efile.getFtpdir());}?核心的就是这2句,1 注册对象,2反序列化Marshaller mashaller = JAXBContext.newInstance(Result.class).createMarshaller();ObjectFactory factory = new ObjectFactory();Result toxmlResult = factory.createResult();//可选 开始Field theField = factory.createResultDocInfoField();theField.setColName("ssss");DocInfo dii = factory.createResultDocInfo();dii.getField().add(theField);toxmlResult.setDocInfo(dii);//可选 结束mashaller.marshal(toxmlResult, new File("c:/outPut.xml"));

核心: 注册对象 和序列化

Marshaller mashaller = ?JAXBContext.newInstance(Result.class).createMarshaller();

mashaller.marshal(toxmlResult, new File("c:/outPut.xml"));

?

?

附源码

读书人网 >XML SOAP

热点推荐