JAXB入门

[quote]jaxb是一个读写xml的工具,还可以提供验证,不需要额外的jar[/quote]

1. XSD sample
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org/draft_2/"
targetNamespace="http://www.example.org/draft_2/">

<xs:complexType name="BaseCommonRefCType">
<xs:sequence>
<xs:element name="name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1" />
<xs:maxLength value="64" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="type" maxOccurs="1"
minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1" />
<xs:maxLength value="64" />
<xs:enumeration value="filter" />
<xs:enumeration value="nzload" />
<xs:enumeration value="db_system" />
<xs:enumeration value="repetition_group" />
<xs:enumeration value="system_path" />
<xs:enumeration value="record" />
<xs:enumeration value="report" />
<xs:enumeration value="mapping" />
<xs:enumeration value="table" />
<xs:enumeration value="cob" />
<xs:enumeration value="file_container" />
<xs:enumeration value="node" />
<xs:enumeration value="instance" />
<xs:enumeration value="stage" />
<xs:enumeration value="java_config" />
<xs:enumeration value="business_date" />
<xs:enumeration value="matrix" />
<xs:enumeration value="database" />
<xs:enumeration value="file_pattern" />
<xs:enumeration value="gemfire_region" />
<xs:enumeration value="task" />
<xs:enumeration value="config_file" />
<xs:enumeration value="dataflow" />
<xs:enumeration value="ems" />
<xs:enumeration value="workflow" />
</xs:restriction>
</xs:simpleType>
</xs:element>

</xs:sequence>
</xs:complexType>
</xs:schema>


2. 根据xsd生成java类
xjc -p com.wilson test.xsd -d   src



3. Sample class for retrieving classes from xml
public class JAXBUtil {
private static final Log logger = LogFactory.getLog(JAXBUtil.class.getName());

public static YourClass retrieveObjectFromXML(File xml, URL xsdURL) {
ProcessLifeCycle lifeCycle = null;
JAXBContext jaxbContext;
ValidationEventCollector vec = new ValidationEventCollector();

try {
jaxbContext = JAXBContext.newInstance(YourClass.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();




unmarshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsdURL));
unmarshaller.setEventHandler(vec);
lifeCycle = (ProcessLifeCycle) unmarshaller.unmarshal(xml);

} catch (JAXBException e) {
logger.error("", e);
} catch (SAXException e) {
logger.error("", e);
} finally {
if (vec != null && vec.hasEvents()) {
for (ValidationEvent ve : vec.getEvents()) {
String msg = ve.getMessage();
ValidationEventLocator vel = ve.getLocator();
int line = vel.getLineNumber();
int column = vel.getColumnNumber();
throw new RuntimeException("Can't unmarshal the XML file, error message: " + " At line " + line + ", column " + column + ": "
+ msg);
}
}
}
return lifeCycle;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值