`

XML<->bean (JXAB)

    博客分类:
  • XML
 
阅读更多
详细内容请参考这里(这是一个更复杂一点的例子,HashMap类型互转,其中就有你需要的bean互转部分):http://www.chinasb.org/archives/2011/04/1556.shtml

package cn.com.farben.cms.app.stockline.util.adapter;

import java.io.File;
import java.util.LinkedHashMap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import cn.com.farben.cms.app.stockline.vo.StockLineVO;
import cn.com.farben.cms.app.stockline.vo.StockVO;

public class Test {

	public static void main(String[] args) throws JAXBException {
		JAXBContext jaxbContext = JAXBContext.newInstance(StockLineVO.class);
		StockLineVO stockLineVO = new StockLineVO();
		stockLineVO.setStockName("新华制药");
		stockLineVO.setStockCode("000756");
		LinkedHashMap&lt;String, StockVO&gt; stockLinkedHashMap = new LinkedHashMap&lt;String, StockVO&gt;();
		stockLinkedHashMap.put("2007010225", new StockVO("2007010225","10.0","10.1","10.2","10.3","10.4","10.5"));
		stockLinkedHashMap.put("2007010226", new StockVO("2007010226","10.0","10.1","10.2","10.3","10.4","10.5"));
		stockLinkedHashMap.put("2007010224", new StockVO("2007010226","10.0","10.1","10.2","10.3","10.4","10.5"));
		stockLineVO.setStockLinkedHashMap(stockLinkedHashMap);
		Marshaller marshaller = jaxbContext.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

		// Output the generated XML:
		marshaller.marshal(stockLineVO, System.out);

		// Save the output to a foo.xml
		File xmlFile = new File("d:\\stock.xml");
		marshaller.marshal(stockLineVO, xmlFile);

		// Restore the Foo class from xml file
		Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		StockLineVO _stockLineVO = (StockLineVO) unmarshaller.unmarshal(xmlFile);

		// See the result
		System.out.println(_stockLineVO.getStockLinkedHashMap().get("2007010224").getDate());

	}
}
 

分享到:
评论

相关推荐

    JXAB进行接口开发解析xml的工具类

    JXAB进行接口开发解析xml的工具类

    java生成实体工具

    修改一个bug,自动生成java实体,使用前请仔细阅读操作说明,如有问题请及时反馈,谢谢。

    大数据培训课程安排.pdf

    主要技术包括:JavaScript、Jquery、注解反射⼀起使⽤,XML以及XML解析、解析dom4j、jxab、jdk8.0新特性、SVN、 Maven、easyui 4. 描述如下: 前两个阶段的基础上化静为动,可以实现让我们⽹页内容更加的丰富,当然...

    WINPE下VISTA/WIN7安装支持

    WINPE下VISTA/WIN7安装支持

    Foxit PDF Editor 2.2.1 build 1102 绿色版

    Foxit PDF Editor 是第一个真正的PDF文件编辑软件。许多人 都希望能找到一个象编辑其它类型的文档的编辑器,事实上在 Foxit PDF Editor 出现之前,根本没有这样的工具。每一个 PDF 文件都包含很多页面,每一页包含...

Global site tag (gtag.js) - Google Analytics