castor Unmarshaller 如何解组namespace的问题

我现在有一个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);
}
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值