一个jaxb的例子看java object和xml之间的转换
这段时间在研究o/x mapping,即Java object和xml之间的转换以及xml文件验证的问题,自己也动手开发了一个小的演示例子,如下所示: --------开发环境----- 1.jdk1.5 2.jwsdp2.0(Java Web Services Developer Pack 2.0, sun的官网上有的下) 3.程序中需要用到的jar包: %jwsdp2.0安装目录下%/jaxb/lib/jaxb-api.jar %jwsdp2.0安装目录下%/jaxb/lib/jaxb-impl.jar %jwsdp2.0安装目录下%/sjsxp/lib/jsr173_api.jar %jwsdp2.0安装目录下%/jwsdp-shared/lib/activation.jar 4.开发工具:eclipse3.2 --------------------- 定义一个XML schema文件--library.xsd [code="xml"] [/code] 再定义jwsdp的xjc工具的binding文件---binding.xjb [code="xml"] [/code] 注意!binding文件的编码格式要用UTF-8,如果直接把代码粘到eclipse里编码格式就不是UTF-8了,这时xjc工具就要报错了,解决办法就是把这个文件用UltraEdit工具转一下。 可以通过binding文件和xsd文件用jaxb自带的xjc工具自动生成java object,是不是很棒啊^_^ 现在写o/x mapping的测试主类----Main.java [code="java"] package com.cuishen.o_x_mapping; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.GregorianCalendar; import java.util.List; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; import javax.xml.bind.ValidationEventLocator; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import com.cuishen.o_x_mapping.object.Book; import com.cuishen.o_x_mapping.object.Librarys; import com.cuishen.o_x_mapping.object.User; import com.cuishen.o_x_mapping.object.ObjectFactory; import com.cuishen.o_x_mapping.utils.FileUtils;; /** * o/x mapping测试主类 * @author cuishen * @date 2008-4-2 * @version 1.0 */ public class Main { public static void object2xml() { try { JAXBContext context = JAXBContext.newInstance("com.cuishen.o_x_mapping.object"); Librarys libs = new Librarys(); List libList = libs.getLibrary(); User usr = createUser("1011", 28, "cuishen"); Book book = createBook("成长比成功更重要", getDate(), "成功励志书", "88-91-211", usr); libList.add(createLibrary("上海图书馆", 2, 4, book)); usr = createUser("1017", 22, "sanmao"); book = createBook("七龙珠", getDate(), "漫画", "34-16-310", usr); libList.add(createLibrary("徐汇图书馆", 3, 11, book)); // create an element for marshalling JAXBElement element = (new ObjectFactory()).createLibrarys(libs); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.setProperty("jaxb.encoding", "gbk"); m.marshal(element, System.out); File xmlFile = FileUtils.makeFile(getXmPath()); OutputStream ot = new FileOutputStream(xmlFile); m.marshal(element, ot); } catch (JAXBException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void xml2object(Unmarshaller u) { try { //JAXBContext jc = JAXBContext.newInstance( "com.cuishen.o_x_mapping.object" ); //Unmarshaller u = jc.createUnmarshaller(); // unmarshal a po instance document into a tree of Java content // objects composed of classes from the com.cuishen.o_x_mapping.object package. JAXBElement poe = (JAXBElement)u.unmarshal(new FileInputStream(getXmPath())); Librarys librarys = (Librarys)poe.getValue(); List libList = librarys.getLibrary(); for(int i = 0; i < libList.size(); i++) { Librarys.Library lib = (Librarys.Library)libList.get(i); System.out.println("librarys ==> "); System.out.println("===> library :"); System.out.println("=========> layer " + lib.getLayer()); System.out.println("=========> name " + lib.getName()); System.out.println("=========> row " + lib.getRow()); System.out.println("=========> book :"); Book book = lib.getBook(); System.out.println("=============> id " + book.getId()); System.out.println("=============> borrowDate " + book.getBorrowDate()); System.out.println("=============> name " + book.getBookName()); System.out.println("=============> desc " + book.getDescription()); System.out.println("=============> user :"); User usr = book.getUser(); System.out.println("==================> userID " + usr.getId()); System.out.println("==================> userName " + usr.getUserName()); System.out.println("==================> Age " + usr.getUserAge()); } } catch( JAXBException je ) { je.printStackTrace(); } catch( IOException ioe ) { ioe.printStackTrace(); } } /** * 对xml文件解组时的验证 * @return boolean */ public static Unmarshaller validate() { // create a JAXBContext capable of handling classes generated into // the com.cuishen.o_x_mapping.object package JAXBContext jc; Unmarshaller u = null; try { jc = JAXBContext.newInstance( "com.cuishen.o_x_mapping.object" ); u = jc.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try { Schema schema = sf.newSchema(new File(getXSDPath())); u.setSchema(schema); u.setEventHandler( new ValidationEventHandler() { public boolean handleEvent(ValidationEvent ve) { if (ve.getSeverity() == ValidationEvent.WARNING || ve.getSeverity() != ValidationEvent.WARNING) { ValidationEventLocator vel = ve.getLocator(); System.out.println("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:" + ve.getMessage()); return false; } return true; } } ); } catch (org.xml.sax.SAXException se) { System.out.println("Unable to validate due to following error."); se.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } } catch (JAXBException e) { e.printStackTrace(); } return u; } private static String getXmPath() throws MalformedURLException { String xmlPath = FileUtils.extractDirPath(FileUtils.extractDirPath(Main.class.getClassLoader().getResource(".").toString())); xmlPath = FileUtils.makeFilePath(xmlPath, "src/com/cuishen/o_x_mapping/xml/library.xml"); URL url = new URL(xmlPath); return url.getPath(); } private static String getXSDPath() throws MalformedURLException { String xsdPath = FileUtils.extractDirPath(FileUtils.extractDirPath(Main.class.getClassLoader().getResource(".").toString())); xsdPath = FileUtils.makeFilePath(xsdPath, "library.xsd"); URL url = new URL(xsdPath); System.out.println("xsd path ==========> " + url.getPath()); return url.getPath(); } public static void main(String args[]) { System.out.println("======== object to xml ..."); object2xml(); System.out.println("======== xml to object ..."); xml2object(validate()); } public static Librarys.Library createLibrary(String name, int layer, int row, Book book) { Librarys.Library library = new Librarys.Library(); library.setName(name); library.setLayer(layer); library.setRow(row); library.setBook(book); return library; } public static Book createBook(String name, XMLGregorianCalendar date, String desc, String id, User usr) { Book book = new Book(); book.setBookName(name); book.setBorrowDate(date); book.setDescription(desc); book.setId(id); book.setUser(usr); return book; } public static User createUser(String id, int age, String name) { User usr = new User(); usr.setId(id); usr.setUserAge(age); usr.setUserName(name); return usr; } private static XMLGregorianCalendar getDate() { try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()); } catch (DatatypeConfigurationException e) { throw new Error(e); } } } [/code] 以上代码的核心是xml文件的编组和解组,用到了接口javax.xml.bind.Marshaller(Marshaller 类负责管理将 Java 内容树序列化回 XML 数据的过程,它提供了基本的编组方法marshal), 以及接口javax.xml.bind.Unmarshaller(Unmarshaller 类管理将 XML 数据反序列化为新创建的 Java 内容树的过程,并可在解组时有选择地验证 XML 数据。它针对各种不同的输入种类提供各种重载的 unmarshal 方法。), 至于xml文件的验证是在解组的时候,调用方法unmarshal会触发验证的机制,验证xml文件是否符合schema的规范。 下面定义build.xml,用ant工具来完成整个项目的编译和运行,方便快捷,^_^ [code="xml"] [/code] 我已经将演示项目的完整代码打包上传到附件了,测试通过,方便网友们下载,有不足之处愿与大家切磋
请问为什么针对这样的schema为什么会生成这样的java类呢? 应该怎么写binding.xjb文件?貌似属性也不行啊
如果已经有Schema的.xsd的文件了,直接用xjc生成Java class就行了把?为什么还需要一个binding.xjb文件
如果library.xsd中包含其他的schema 应该怎么设置Unmarshaller的schema?
jaxb处理xml得到java object,当这个object中封装的数据发生变化后,调用上例中方法object2xml()将这个object写到xml里面覆盖原来的xml不就行啦
用jaxb处理XML得到的java对象的变化能不能反映到Xml中
很好的例子,谢谢分享!