我现在有一个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 Unmarshaller 如何解组namespace的问题
猜你喜欢
转载自zgdkik.iteye.com/blog/1934647
今日推荐
周排行