论坛首页 Java企业应用论坛

castor Unmarshaller 如何解组namespace的问题

浏览 2055 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-08-31  
我现在有一个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);
}
}
}




论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics