xml与java对象映射


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类文件,然后在项目中导入这些类。

 

需要引用一下类:

 

Java代码   收藏代码
  1. import javax.xml.bind.JAXBContext;  
  2. import javax.xml.bind.JAXBException;  
  3. import javax.xml.bind.Marshaller;  
  4. import javax.xml.bind.Unmarshaller;  
 

 

 

A. 将XML映射为java对象

Java代码   收藏代码
  1. // 建立上下文 amos.note为编译器生成的类文件的包名称  
  2. JAXBContext jaxbContext = JAXBContext.newInstance("amos.note");  
  3. // 建立unmashaller对象  
  4. Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();  
  5. // 将xml文件映射为java对象  
  6. Note note = (Note) unmarshaller.unmarshal(new File("e:/t/t.xml"));  
  7. // TODO ....  

 以上代码片段就是将t.xml文件中的根Note节点映射为了Note对象,也可以将其他形式的xml进行映射。

 

 

B. 将java对象映射为XML

Java代码   收藏代码
  1. // 准备要映射的对象 amos.note.ObjectFactory是编译器生成的类  
  2. amos.note.ObjectFactory objFactory = new amos.note.ObjectFactory();  
  3. Note note = objFactory.createNote();  
  4. //上面的note对象也可是A部分中的那个note对象,这样A、B两个步骤和在一起就是对xml文件的更新了  
  5. // 建立上下文 amos.note为编译器生成的类文件的包名称  
  6. JAXBContext jaxbContext = JAXBContext.newInstance("amos.note");  
  7. // 建立mashaller对象  
  8. Marshaller marshaller = jaxbContext.createMarshaller();  
  9. // 将java对象映射为xml文件  
  10. marshaller.marshal(note, new FileOutputStream("e:/t/t.xml"));  

 以上代码片段就是将Note对象映射为t.xml文件中的根Note节点,也可以映射成为其他形式的对象。

 

 

基于XSDXML文档校验

 

参考: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 使用三个类来验证文档:SchemaFactorySchema 和 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 对象。

 

需要导入的包:

 

Java代码   收藏代码
  1. import javax.xml.transform.Source;  
  2. import javax.xml.transform.stream.StreamSource;  
  3. import javax.xml.validation.Schema;  
  4. import javax.xml.validation.SchemaFactory;  
  5. import javax.xml.validation.Validator;  
 

 

下面是验证的主要代码:

Java代码   收藏代码
  1. /** 
  2.      * 根据xsd文件校验一个xml文件是否有效,当无效异常时交由errorHandler处理异常 
  3.      *  
  4.      * @param xmlFilePath 
  5.      * @param xsdFilePath 
  6.      * @param errorHandler 
  7.      * @return 
  8.      * @throws IOException 
  9.      * @throws SAXException 
  10.      */  
  11.     private boolean doValidate(String xmlFilePath, String xsdFilePath,  
  12.             ErrorHandler errorHandler) throws IOException, SAXException {  
  13.         boolean rt = false;  
  14.         // 1. Lookup a factory for the W3C XML Schema language  
  15.         SchemaFactory factory = SchemaFactory  
  16.                 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);  
  17.         // 2. Compile the schema.  
  18.         // Here the schema is loaded from a java.io.File, but you could use  
  19.         // a java.net.URL or a javax.xml.transform.Source instead.  
  20.         File schemaLocation = new File(xsdFilePath);  
  21.         Schema schema = factory.newSchema(schemaLocation);  
  22.   
  23.         // 3. Get a validator from the schema.  
  24.         Validator validator = schema.newValidator();  
  25.         validator.setErrorHandler(errorHandler);  
  26.   
  27.         // 4. Parse the document you want to check.  
  28.         Source source = new StreamSource(xmlFilePath);  
  29.   
  30.         // 5. Check the document  
  31.         try {  
  32.             validator.validate(source);  
  33.             rt = true;  
  34.         } catch (SAXException ex) {  
  35.             rt = false;  
  36.         }  
  37.         return rt;  
  38.     }  

需要注意的是,当传入有效的ErrorHandler示例后,执行validator.validate(source)方法时无论是否出现异常都不在抛出SAXException异常。

 

因此,下面的方法包装上面的方法来验证

 

Java代码   收藏代码
  1. /** 
  2.      * 根据xsd文件校验一个xml文件是否有效 
  3.      * @param xmlFilePath 
  4.      * @param xsdFilePath 
  5.      * @return true-有效 false-无效 
  6.      * @throws IOException 
  7.      * @throws SAXException 
  8.      */  
  9.     public boolean validate(String xmlFilePath, String xsdFilePath) throws IOException, SAXException{  
  10.         return this.doValidate(xmlFilePath, xsdFilePath, null);  
  11.     }  
  12.   
  13.       
  14.     /** 
  15.      * 根据xsd文件校验一个xml文件是否有效,当无效异常时交由errorHandler处理异常 
  16.      * @param xmlFilePath 
  17.      * @param xsdFilePath 
  18.      * @param errorHandler 
  19.      * @throws IOException 
  20.      * @throws SAXException 
  21.      */  
  22.     public void validate(String xmlFilePath, String xsdFilePath, ErrorHandler errorHandler) throws IOException, SAXException{  
  23.         boolean rt = this.doValidate(xmlFilePath, xsdFilePath, errorHandler);  
  24.     }  

 

 

其实,验证的这个步骤不必单独占用一些列资源而只做这一件事,可以在上面的映射过程中完成,oracle网站上提供了验证的说明,但是我自己测试发现他说的方法已经废弃了,因此,没有在映射的代码中提到,还有待研究。

 

附件xml.zip是完整的示例程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值