1 Star 0 Fork 0

LiShixi / pim

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TestJAXB 1.98 KB
一键复制 编辑 原始数据 按行查看 历史
package jaxb;
import java.io.File;
import java.time.LocalDate;
import java.time.Month;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author LiShixi
*/
public class TestJAXB {
public static void main(String[] args) {
TestJAXB testJAXB = new TestJAXB();
testJAXB.toXml();
testJAXB.readXml();
}
public void toXml() {
DemoBeanListWrapper lw = new DemoBeanListWrapper();
for (int i = 0; i < 37; i++) {
DemoBean dd = new DemoBean();
dd.setAge(1);
dd.setName("test" + i);
dd.setBirthday(LocalDate.of(1990, Month.MAY, 6));
lw.getList().add(dd);
}
try {
JAXBContext context = JAXBContext.newInstance(DemoBeanListWrapper.class);
Marshaller cm = context.createMarshaller();
File xmlfile = new File("lsx.xml");
cm.marshal(lw, xmlfile);
} catch (JAXBException ex) {
Logger.getLogger(TestJAXB.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void readXml() {
try {
JAXBContext ctx = JAXBContext.newInstance(DemoBeanListWrapper.class);
Unmarshaller cu = ctx.createUnmarshaller();
DemoBeanListWrapper dl = (DemoBeanListWrapper)cu.unmarshal(new File("lsx.xml"));
int size = dl.getList().size();
System.out.println("size = " + size);
} catch (JAXBException ex) {
Logger.getLogger(TestJAXB.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Java
1
https://gitee.com/loyy77/pim.git
git@gitee.com:loyy77/pim.git
loyy77
pim
pim
master

搜索帮助