我正在尝试使用JibX将XML转换为POJO。下面是我的主要方法:
IBindingFactory bfact = BindingDirectory.getFactory(Asset.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
FileInputStream inputStream = new FileInputStream("NewSample.xml");
Object obj = uctx.unmarshalDocument(inputStream, null);
Asset asset = (Asset)obj;在尝试这样做时,我得到了如下异常:
Unable to access binding information for class com.adaequare.model.document.Asset
Make sure the binding has been compiled在谷歌搜索之后,我发现了问题所在。绑定必须在JiBX绑定编译器的帮助下编译成类文件。在Jibx站点上提到,org.jibx.binding.Compile完成了这种绑定。所以我像这样修改了我的main方法:
package com.adaequare.client;
import org.jibx.binding.Compile;
import org.jibx.binding.Utility;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
import com.adaequare.model.document.Asset;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
public class JibxTest {
public static void main(String[] args) throws IOException {
try {
Compile compiler = new Compile();
compiler.compile(Utility.getClassPaths(), new String[]{"src/main/resources/jibx/AssetBinding.xml"});
IBindingFactory bfact = BindingDirectory.getFactory(Asset.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
FileInputStream inputStream = new FileInputStream("NewSample.xml");
Object obj = uctx.unmarshalDocument(inputStream, null);
Asset asset = (Asset)obj;
} catch (Exception e) {
e.printStackTrace();
}
}
}在运行此命令时,我得到了以下异常:
Error running binding compiler
java.lang.NullPointerException
at org.jibx.binding.classes.MungedClass.checkDirectory(MungedClass.java:241)
at org.jibx.binding.classes.BoundClass.getGenericMunge(BoundClass.java:468)
at org.jibx.binding.classes.BoundClass.getInstance(BoundClass.java:417)
at org.jibx.binding.classes.BoundClass.getInstance(BoundClass.java:501)
at org.jibx.binding.def.ObjectBinding.<init>(ObjectBinding.java:294)
at org.jibx.binding.def.BindingBuilder.unmarshalObjectBinding(BindingBuilder.java:1150)
at org.jibx.binding.def.BindingBuilder.unmarshalMapping(BindingBuilder.java:1888)
at org.jibx.binding.def.BindingBuilder.unmarshalMappings(BindingBuilder.java:1243)
at org.jibx.binding.def.BindingBuilder.unmarshalBindingDefinition(BindingBuilder.java:2245)
at org.jibx.binding.Utility.loadBinding(Utility.java:300)
at org.jibx.binding.Utility.loadFileBinding(Utility.java:420)
at org.jibx.binding.Compile.compile(Compile.java:217)
at com.adaequare.client.JibxTest.main(JibxTest.java:31)当我检查MungedClass源代码时,它实际上是在查找名称以“JiBX_”开头的已编译类文件。由于没有符合条件的文件,因此生成的文件数组为空。当数组长度被访问时,会抛出NPE异常。
我加入compiler.compile的目的是为了运行Jibx绑定编译器。但是它仍然希望文件已经被它编译过了。我是不是漏掉了什么?
作为运行主类的另一种选择,我也在build.gradle中定义了它。这就是它:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'jetty'
sourceCompatibility = 1.7
version = 1.0
repositories {
mavenCentral()
}
ext.springVersion = '4.0.2.RELEASE';
ext.jerseyVersion = '2.6';
ext.jettyVersion = '9.1.3.v20140225';
ext.jibxVersion = "1.2.5";
httpPort = 8099;
def springDependencies = ["org.springframework:spring-core:$springVersion",
"org.springframework:spring-beans:$springVersion",
"org.springframework:spring-context:$springVersion",
"org.springframework:spring-web:$springVersion",
"org.springframework:spring-webmvc:$springVersion",
"org.springframework:spring-oxm:$springVersion"]
def jettyDependencies = ["org.eclipse.jetty:jetty-server:$jettyVersion",
"org.eclipse.jetty:jetty-servlet:$jettyVersion"]
def jerseyDependencies = ["org.glassfish.jersey.containers:jersey-container-servlet:$jerseyVersion",
'javax.ws.rs:javax.ws.rs-api:2.0']
def jibxDependencies = ["org.jibx:jibx-run:$jibxVersion",
"org.jibx:jibx-bind:$jibxVersion",
"org.jibx:jibx-extras:$jibxVersion"]
ext.libs = [
springDependencies : springDependencies,
jerseyDependencies : jerseyDependencies,
jibxDependencies : jibxDependencies,
guava: 'com.google.guava:guava:16.0.1',
junit: 'junit:junit:4.11'
]
configurations { jibx }
dependencies {
compile springDependencies;
compile jibxDependencies;
compile jerseyDependencies;
compile("org.glassfish.jersey.ext:jersey-spring3:$jerseyVersion"){
exclude group: 'org.springframework'
};
compile libs.guava;
testCompile libs.junit;
jibx libs.jibxDependencies;
}
task(type : JavaExec, 'generateBinding') {
classpath = configurations.jibx + configurations.compile + sourceSets.main.runtimeClasspath
main = 'org.jibx.binding.Compile'
args = [
'src/main/resources/jibx/AssetBinding.xml'
]
}
compileJava.doLast{
tasks.generateBinding.execute();
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}在运行'gradle clean build‘时,它也抛出了与上面相同的异常。不确定我在这里错过了什么?
发布于 2014-03-15 13:23:58
Prasanth,你为什么不让你的生活变得简单一点,从JiBX项目或我们的github网站上的一个简单的例子开始。
我建议你从这个例子开始:
https://github.com/jibx/maven-plugin/tree/master/test-suite/base-binding-test
这个项目中的示例有一个简单的程序来编组和解组xml。
祝好运!
完成
JiBX贡献者
https://stackoverflow.com/questions/22407360
复制相似问题