浏览 2054 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2013-08-31
<?xml version="1.0" encoding="UTF-8"?> <InstructionFileBean xmlns:n="http://www.hw.cn/pool" xmlns="http://www.hw.cn"> <InstructionBeans> <InstructionCode>liu</InstructionCode> <FunAndSerach> <code>yi</code> </FunAndSerach> </InstructionBeans> <InstructionBeans> <InstructionCode>liu</InstructionCode> <FunAndSerach> <code>er</code> </FunAndSerach> </InstructionBeans> </InstructionFileBean> 现在通过castor解组, mapping.xml文件如下: <?xml version="1.0"?> <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" "http://castor.org/mapping.dtd"> <mapping> <class name="FunAndSerach"> <map-to xml="FunAndSerach" /> <field name="code" type="java.lang.String"> <bind-xml name="code" node="element"/> </field> </class> <class name="InstructionBean"> <map-to xml="InstructionBean" /> <field name="instructionCode" type="java.lang.String"> <bind-xml name="InstructionCode" node="element"/> </field> <field name="instructionDomin" type="InstructionDomin"> <bind-xml name="instructionDomin" auto-naming="deriveByClass" node="element"/> </field> </class> <class name="InstructionFileBean"> <map-to xml="InstructionFileBean"/> <field name="instructionBeans" type="InstructionBean" collection="arraylist"> <bind-xml name="InstructionBeans" node="element"/> </field> </class> </mapping> 在解组时得不到对像, public class InstrUnmarshaller { public static void main(String[] args) { Mapping mapping = new Mapping(); try { /** * Unmarshaller unmar = new Unmarshaller(mapping); MyOrder order = (MyOrder) unmar.unmarshal(new InputSource( new FileReader("order.xml"))); */ mapping.loadMapping("instruction_map.xml"); //mapping.loadMapping("author_map.xml"); //XMLContext context = new XMLContext(); //context.addMapping(mapping); //Unmarshaller unmarshaller = context.createUnmarshaller(); Unmarshaller unmarshaller = new Unmarshaller(mapping); unmarshaller.addNamespaceToPackageMapping("http://www.szse.cn/pool", "n"); unmarshaller.addNamespaceToPackageMapping("http://www.szse.cn/", ""); //unmarshaller.setProperty("xmlns", "http://www.w3.org/TR/html4/"); //FileReader reader = new FileReader(new File("D:\\digitnexus\\castor\\bookdata.xml")); FileReader reader = new FileReader("map_marshaller.xml"); InstructionFileBean book = (InstructionFileBean) unmarshaller.unmarshal(reader); List<InstructionBean> authors = book.getInstructionBeans(); for (Iterator<InstructionBean> i = authors.iterator(); i.hasNext();) { InstructionBean author = (InstructionBean) i.next(); System.out.println("Author: " + author.getInstructionCode() + " " + author.getInstructionDomin().getClass().getSimpleName()); } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |