我现在有一个XML文件,如下:
<?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);
}
}
}
分享到:
相关推荐
没法解组,应用castor技术,test包是测试包,里面只有model和map映射,很简单的小测试工程
使用Castor时,如果没有根节点时,会报出异常,这个例子就是为了解决这个问题
castor castor资料 castor castor castor
org.castor.util.IdentityMap org.castor.util.IdentitySet org.exolab.javasource.JEnum org.exolab.javasource.JType org.exolab.castor.util.List org.exolab.javasource.Header org.exolab.javasource.JClass ...
castor 帮助文档 Castor API Documentation
castor简介 castor是一种将java对象和XML自动绑定的开源软件。它可以在java对象、XML文本、SQL数据表以及LDAP目录之间绑定。Castor几乎是JAXB的替代品。Castor是ExoLab Group下面的一个开放源代码的项目,它主要实现...
完整jar 直接导入使用 Castor 是一种将Java对象和XML自动绑定的开源软件。它可以在Java对象、XML文本、SQL数据表以及LDAP目录之间绑定。
castor1.4 xsd生成java
eclipse中castor插件的集成,方便数据绑定的实现
castor学习教程 castor学习教程castor学习教程 castor学习教程 castor学习教程
castor-1.2.jar castor-1.2-anttasks.jar castor-1.2-codegen.jar castor-1.2-ddlgen.jar castor-1.2-jdo.jar castor-1.2-xml-schema.jar castor-1.2-xml.jar
castor解析xml
castor-1.2.zip
Castor 项目采用 BSD 类型的证书,因此可在任何类型的应用程序(包括完整版权的项目)中使用。 Castor 实际上仅仅有 XML 数据绑定,它还支持 SQL 和 LDAP 绑定
castor解析xml与xsd校验xml
用于javabean到xml映射文件的转化,可用于castor框架。 JDK6编译、运行。 双击运行时,需要等待一段时间。
Castor是ExoLab Group下面的一个开放源代码的项目,它主要实现的是O/R映射功能。它主要API和数据接口为:JDO-like, SQL, OQL, JDBC, LDAP, XML, DSML。它支持分布式目录事务处理和时间;提供处理XML、Directory、...
Castor 项目采用 BSD 类型的证书,因此可在任何类型的应用程序(包括完整版权的项目)中使用。 Castor 实际上仅仅有 XML 数据绑定,它还支持 SQL 和 LDAP 绑定
castor-0.9.5.3-xml.jar,java和xml互相转换所使用的JAR
castor 开发指南 xml xml转换 序列化 用Castor来完成Java对象到xml的相互转换。