JAXB提供了XML到java对象(类)之间的相互映射(转换),我们可以利用JAXB来消除繁琐的XML解析工作。
下面是引用oracle网站中有关jaxb的一句话。
Now developers have another Java API at their disposal that can make it easier to access XML documents: Java Architecture for XML Binding (JAXB). A Reference Implementation of the API is now available in the Java Web Services Developer Pack V 1.1.
JAXB包括两部分:
1.编译器程序:负责根据XML Schema(xsd文件)编译生成对应java类文件
2.JAXB API:程序中调用这些api,再结合1中的类达到映射的目的
对于JAXB的安装及编译器部分的使用请参见:http://maimode.iteye.com/admin/blogs/1329929
本篇将主要针对api部分给出代码示例,并在后文中给出文档校验的方法。
无论是将xml映射为java对象还是将java对象映射为xml,都首先要使用JAXB的编译器将XSD文件编译成java类文件,然后在项目中导入这些类。
需要引用一下类:
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- import javax.xml.bind.Unmarshaller;
A. 将XML映射为java对象
- // 建立上下文 amos.note为编译器生成的类文件的包名称
- JAXBContext jaxbContext = JAXBContext.newInstance("amos.note");
- // 建立unmashaller对象
- Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
- // 将xml文件映射为java对象
- Note note = (Note) unmarshaller.unmarshal(new File("e:/t/t.xml"));
- // TODO ....
以上代码片段就是将t.xml文件中的根Note节点映射为了Note对象,也可以将其他形式的xml进行映射。
B. 将java对象映射为XML
- // 准备要映射的对象 amos.note.ObjectFactory是编译器生成的类
- amos.note.ObjectFactory objFactory = new amos.note.ObjectFactory();
- Note note = objFactory.createNote();
- //上面的note对象也可是A部分中的那个note对象,这样A、B两个步骤和在一起就是对xml文件的更新了
- // 建立上下文 amos.note为编译器生成的类文件的包名称
- JAXBContext jaxbContext = JAXBContext.newInstance("amos.note");
- // 建立mashaller对象
- Marshaller marshaller = jaxbContext.createMarshaller();
- // 将java对象映射为xml文件
- marshaller.marshal(note, new FileOutputStream("e:/t/t.xml"));
以上代码片段就是将Note对象映射为t.xml文件中的根Note节点,也可以映射成为其他形式的对象。
基于XSD的XML文档校验
参考:http://www.ibm.com/developerworks/cn/xml/x-javaxmlvalidapi.html
Java 5 引入了 javax.xml.validation 包,提供了独立于模式语言的验证服务接口。这个包也可用于 Java 1.3 及更高版本,不过要单独安装 JAXP 1.3。其他产品中,Xerces 2.8 包含了这个库的实现。
javax.xml.validation API 使用三个类来验证文档:SchemaFactory、Schema 和 Validator。还大量使用了 TrAX 的javax.xml.transform.Source 接口来表示 XML 文档。简言之,SchemaFactory 读取模式文档(通常是 XML 文件)并创建 Schema 对象。Schema 创建一个 Validator 对象。最后,Validator 对象验证表示为 Source 的 XML 文档。
改变验证所依据的模式、要验证的文档甚至使用的模式语言都很简单。但无论什么情况,验证都需要经过下列五个步骤:
1) 为编写模式所用的语言加载一个模式工厂。
2) 编译源文件中的模式。
3) 用编译后的模式创建一个验证程序。
4) 为需要验证的文档创建 Source 对象。StreamSource 通常最简单。
5) 验证输入的源文档。如果文档无效,validate() 方法将抛出 SAXException。否则什么也不显示。
可以反复使用同一个验证程序和同一个模式多次。但是所有类都不是线程安全的或者可重入的。如果用多个线程同时验证,一定要保证每个线程有自己的 Validator 和 Schema 对象。
需要导入的包:
- import javax.xml.transform.Source;
- import javax.xml.transform.stream.StreamSource;
- import javax.xml.validation.Schema;
- import javax.xml.validation.SchemaFactory;
- import javax.xml.validation.Validator;
下面是验证的主要代码:
- /**
- * 根据xsd文件校验一个xml文件是否有效,当无效异常时交由errorHandler处理异常
- *
- * @param xmlFilePath
- * @param xsdFilePath
- * @param errorHandler
- * @return
- * @throws IOException
- * @throws SAXException
- */
- private boolean doValidate(String xmlFilePath, String xsdFilePath,
- ErrorHandler errorHandler) throws IOException, SAXException {
- boolean rt = false;
- // 1. Lookup a factory for the W3C XML Schema language
- SchemaFactory factory = SchemaFactory
- .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
- // 2. Compile the schema.
- // Here the schema is loaded from a java.io.File, but you could use
- // a java.net.URL or a javax.xml.transform.Source instead.
- File schemaLocation = new File(xsdFilePath);
- Schema schema = factory.newSchema(schemaLocation);
- // 3. Get a validator from the schema.
- Validator validator = schema.newValidator();
- validator.setErrorHandler(errorHandler);
- // 4. Parse the document you want to check.
- Source source = new StreamSource(xmlFilePath);
- // 5. Check the document
- try {
- validator.validate(source);
- rt = true;
- } catch (SAXException ex) {
- rt = false;
- }
- return rt;
- }
需要注意的是,当传入有效的ErrorHandler示例后,执行validator.validate(source)方法时无论是否出现异常都不在抛出SAXException异常。
因此,下面的方法包装上面的方法来验证
- /**
- * 根据xsd文件校验一个xml文件是否有效
- * @param xmlFilePath
- * @param xsdFilePath
- * @return true-有效 false-无效
- * @throws IOException
- * @throws SAXException
- */
- public boolean validate(String xmlFilePath, String xsdFilePath) throws IOException, SAXException{
- return this.doValidate(xmlFilePath, xsdFilePath, null);
- }
- /**
- * 根据xsd文件校验一个xml文件是否有效,当无效异常时交由errorHandler处理异常
- * @param xmlFilePath
- * @param xsdFilePath
- * @param errorHandler
- * @throws IOException
- * @throws SAXException
- */
- public void validate(String xmlFilePath, String xsdFilePath, ErrorHandler errorHandler) throws IOException, SAXException{
- boolean rt = this.doValidate(xmlFilePath, xsdFilePath, errorHandler);
- }
其实,验证的这个步骤不必单独占用一些列资源而只做这一件事,可以在上面的映射过程中完成,oracle网站上提供了验证的说明,但是我自己测试发现他说的方法已经废弃了,因此,没有在映射的代码中提到,还有待研究。
附件xml.zip是完整的示例程序