尽量少使用jaxb 进行xml与对象互换
问题不少,这个好处在于方便,不用管他们之间的顺序。
xjc 命令 xjc aa.xsd -p com.aa -d work -b ff.xml
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:extensionBindingPrefixes="inheritance"
version="2.1">
<jaxb:bindings
schemaLocation="c:/a.xsd"
node="/xsd:schema/xsd:element[@name='root_tagname']"
<jaxb:class name="RootClassName" />
</jaxb:bingdings>
</jaxb:bindings>
write:
JAXBContext context = JAXBContent.newInstance(bean.getClass().getPackage().getName),Thread.currentThread().getContextClassLoader());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT,false);
marshaller.marshal(bean,new File(filePath));
============================
read:
JAXBContext context = JAXBContent.newInstance(bean.getClass().getPackage().getName),Thread.currentThread().getContextClassLoader());
Unmarshaller unmarshaller = context.createUnmarshaller();
Object obj = unmarshaller.unmarshal(new File(filePath));
对于生成的JAXBElement对象实际过程中可将此对象替换为具体的类型的对象(String,Long,Boolean等)方便界面编辑保存。