我想使用Jibx来解组以下XML(存储在名为test.xml的文件中):
我定义了模式(在一个名为simple.xsd的文件中),如下所示:
使用org.jibx.schema.codegen.CodeGen工具从中生成Java文件并编写此测试程序:
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
import my.target.ns.RootElement;
public final class Program {
public static void main(final String[] args) {
try {
IBindingFactory bfact = BindingDirectory.getFactory(RootElement.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
FileInputStream in = new FileInputStream(new File("test.xml"));
RootElement data = (RootElement) uctx.unmarshalDocument(in, null);
// This is not what I was expecting. I was expecting
// List (or equivalent) not
// a single RootElement.Enumeration instance
RootElement.Enumeration attrValue = data.getAttrWithEnum();
System.out.println(attrValue);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
该程序失败并显示错误:
org.jibx.runtime.JiBXException: No match found for value ‘avalue anothervalue’ in enum class my.target.ns.RootElement$Enumeration
如果我像这样调整我的输入XML(即只设置一个枚举值)它可以工作(打印AVALUE).
所以,似乎jibx不喜欢我想要允许枚举值列表(我希望getAttrWithEnum返回一个集合,但它返回一个对象 – 请参阅上面代码示例中的注释).
当我使用jaxb(使用xjc生成java文件)时,相同的XSD工作正常,所以我认为我的XSD是有效的(尽管如果有更好的方法来定义我想要的东西,那就没问题).
因此,我的问题是:
如何使用允许jibx中的多个枚举值的属性来解组XML文档?