转换Xml与java object,一般使用JDom或SAX。但这样手动mapping,代码实在冗长乏味。目前已有jaxb,castor, xstream等类库提供自动映射,如Xfire的webservice就是基于这些类库进行xml解组.
1.已定义xml模板文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<job-config>
<purge-interval>3</purge-interval>
<db-info>
<dbtype>Oracle</dbtype>
<url>oracle:thin:localhost:orcl</url>
<username>admin</username>
<password>admin</password>
</db-info>
<job-info>
<frequency>
<interval-type>WEEKLY</interval-type>
<interval>10</interval>
<start-time>09:00</start-time>
</frequency>
<objects-component>
<object>
<object-name>RUNTIME</object-name>
<retention>10</retention>
</object>
<object>
<object-name>BUILDTIME</object-name>
<retention>20</retention>
</object>
</objects-component>
<objects-specific>
<object>
<object-name>GOOD</object-name>
<retention>10</retention>
</object>
<object>
<object-name>BUILDTIME</object-name>
<retention>20</retention>
</object>
</objects-specific>
</job-info>
</job-config>
2.产生xsd文件
手动编写xsd当然很不方便,需要你学习太多xsd的语法细节,在我看来使用时可以参考即可。有一些工具可以自动产生xsd文件。譬如MS Visustido提供了xsd.exe,也有其他开源类库(trang)。
这份代码是使用xsd基于xml来产生xsd,
"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe" E:\temp\JobsConfig.xml /outputdir:/outputdir:e:/temp
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
. . .
<xs:element name="job-config">
<xs:complexType>
<xs:sequence>
<xs:element name="purge-interval" type="xs:string" minOccurs="0" />
<xs:element name="db-info" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="dbtype" type="xs:string" minOccurs="0" />
<xs:element name="url" type="xs:string" minOccurs="0" />
<xs:element name="username" type="xs:string" minOccurs="0" />
<xs:element name="password" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- maxOccurs根据需要从unbounded调整为1,则产生的对象从默认的list转为object -->
<xs:element name="job-info" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="frequency" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="interval-type" type="xs:string" minOccurs="0" />
<xs:element name="interval" type="xs:string" minOccurs="0" />
<xs:element name="start-time" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="objects-component" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="objects-specific" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element ref="object" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
. . .
</xs:element>
3.运行xjc命令由xsd生成java对象
可将JAXB的xjc.bat配置为Eclipse的external tool.对象如下,
4.依据xsd格式从xml中读取数据,unmarshal
File xmlDocument = new File("res/JobsConfig.xml");
JAXBContext jaxbContext = JAXBContext.newInstance("generated");
Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = schemaFactory.newSchema(new File("res/JobsConfig.xsd"));
unMarshaller.setSchema(schema);
JobConfig config = (JobConfig) unMarshaller.unmarshal(xmlDocument);
System.out.println("purge-interval: " + config.getPurgeInterval());
DbInfo dbInfo = config.getDbInfo();
System.out.println("DB type:" + dbInfo.getDbtype());
System.out.println("Url type:" + dbInfo.getUrl().trim());
System.out.println("Username type:" + dbInfo.getUsername());
System.out.println("Password type:" + dbInfo.getPassword());
JobInfo jobInfo = config.getJobInfo();
System.out.println("Frequency:" + jobInfo.getFrequency().getStartTime());
}
catch (JAXBException e)
{
System.out.println(e.toString());
}
catch (SAXException e)
{
System.out.println(e.toString());
}
5.构造java对象,产生xml数据,marshal
File xmlDocument = new File("jobConfig.xml");
JAXBContext jaxbContext = JAXBContext.newInstance("generated");
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(
Marshaller.JAXB_FORMATTED_OUTPUT,
true);
ObjectFactory factory = new ObjectFactory();
JobConfig config = factory.createJobConfig();
config.setPurgeInterval("3");
// construct db info
DbInfo dbInfo = factory.createJobConfigDbInfo();
dbInfo.setDbtype("Oracle");
dbInfo.setUrl("oracle:thin:localhost:orcl");
dbInfo.setUsername("admin");
dbInfo.setPassword("admin");
config.setDbInfo(dbInfo);
// construct job info
JobInfo jobInfo = factory.createJobConfigJobInfo();
// job info - frequency
Frequency frequency = factory.createJobConfigJobInfoFrequency();
frequency.setInterval("10");
frequency.setIntervalType("WEEKLY");
frequency.setStartTime("09:00");
jobInfo.setFrequency(frequency);
// job info - ObjectsComponent
ObjectsComponent objectComponet = factory.createJobConfigJobInfoObjectsComponent();
Object runtimeObj = factory.createObject();
runtimeObj.setObjectName("RUNTIME");
runtimeObj.setRetention("10");
objectComponet.getObject().add(
runtimeObj);
Object buildtimeObj = factory.createObject();
buildtimeObj.setObjectName("BUILDTIME");
buildtimeObj.setRetention("20");
objectComponet.getObject().add(
buildtimeObj);
jobInfo.setObjectsComponent(objectComponet);
// job info - ObjectsSpecific
ObjectsSpecific objectSpecfic = factory.createJobConfigJobInfoObjectsSpecific();
Object tObj1 = factory.createObject();
tObj1.setObjectName("GOOD");
tObj1.setRetention("10");
objectSpecfic.getObject().add(
tObj1);
Object tObj2 = factory.createObject();
tObj2.setObjectName("FAT");
tObj2.setRetention("20");
objectSpecfic.getObject().add(
buildtimeObj);
jobInfo.setObjectsSpecific(objectSpecfic);
config.setJobInfo(jobInfo);
// marshal to file
marshaller.marshal(
config,
new FileOutputStream(xmlDocument));
// marshal to console
marshaller.marshal(
config,
System.out);
}
catch (IOException e)
{
System.out.println(e.toString());
}
catch (JAXBException e)
{
System.out.println(e.toString());
}
分享到:
相关推荐
如何更好地jaxb使用如何更好地jaxb使用如何更好地jaxb使用如何更好地jaxb使用
JAXB使用Socket流进行双向通信源码
JAXB框架,作用是对象和XML之间相互转化,本文档包含代码示例,介绍对象和XML如果使用JAXB相互转化 在代码里面备注了具体使用的文字说明
NULL 博文链接:https://dannyhz.iteye.com/blog/2316638
1. 什么是JAXB? Java Architecture for XML Binding (JAXB) 是一个业界的标准,是一项可以根据XML
JAXB的使用JAXB的使用JAXB的使用JAXB的使用
java -jar JAXB2_20060607.jar On Windows, you can just double-click the jar file to execute. Release Notes Browse the release notes online, including what's new. Technical Support Please subscribe to...
NULL 博文链接:https://rensanning.iteye.com/blog/2378681
无需安装,解压后即可。jaxb-api.jar,jaxb-xjc.jar,jaxb-impl.jar,activation.jar等相关jar包在lib文件夹下。
Jaxb annotation 使用 Jaxb annotation初步使用
本人在公司用到了jaxb技术,这是在公司的学习文档,今天跟高兴个大家分享。
在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个错误,按照打出的信息Use the endorsed directory mechanism to place jaxb-api.jar in ...
如何使用JAXB框架定制Web服务行为.doc
我刚写好的 一个简单的小程序 动行里面的read文件 是在idea 中用jaxb 读xml中数据
最新的JAXB API手册! JAXB_2.2_API.chm
使用jaxb生成XML例子,含有例子和注解解析
jaxb-api jaxb-impl jar
JAXB的安装包及插件
activation.jar jaxb1-impl.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jsr173_1.0_api.jar系列jar下载
赠送jar包:jackson-module-jaxb-annotations-2.2.3.jar; 赠送原API文档:jackson-module-jaxb-...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结