Jibx 总是给出“验证期间错误:null”

发布于 2024-07-26 00:20:02 字数 687 浏览 4 评论 0原文

我真的被这个极其简单的映射难住了。 它甚至看起来就像其中一个例子。 如果我注释掉内部结构,它将成功运行绑定编译器。 如果我把内部结构放回去,它就会失败。 请注意,内部结构只是定义 XML。 这基本上是 JIBX 教程示例的 example5。

<binding>
  <mapping name="RequestTransaction" class="TransactionRequest">
    <value name="version" set-method="setVersion" get-method="getVersion" style="attribute" />
    <structure name="transHeader">
      <value name="requestCount" set-method="setRequestCount" get-method="getRequestCount"/>
    </structure>
  </mapping>
 <binding>

然后我在 jibx 编译时收到以下错误:

错误:验证期间错误:null; 关于映射元素(jibx-binding.xml 中第 2 行,第 97 栏),

我完全被难住了,没有想法。 谷歌没有显示任何有用的东西。

I'm really stumped on this incredibly simple mapping. It looks just like one of the examples even. If I comment out the internal structure, it'll run the binding compiler successfully. If I put the internal structure back in, it fails. Note that the internal structure is just defining the XML. This is basically example5 of the JIBX tutorial examples.

<binding>
  <mapping name="RequestTransaction" class="TransactionRequest">
    <value name="version" set-method="setVersion" get-method="getVersion" style="attribute" />
    <structure name="transHeader">
      <value name="requestCount" set-method="setRequestCount" get-method="getRequestCount"/>
    </structure>
  </mapping>
 <binding>

Then I get the following error on the jibx compile:

Error: Error during validation: null; on mapping element at (line 2, col 97, in jibx-binding.xml)

I'm absolutely stumped and out of ideas. Google shows nothing useful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雪碎 2024-08-02 00:20:02

可以说是 JiBX 绑定中最重要的概念,因为它允许您将任意 XML 映射到 Java 类,而无需强迫您创建臃肿且丑陋的嵌套 Java 对象和类层来匹配XML 设计。

在这种情况下,您的绑定声明您有一个名为 的 XML 元素,该元素不会出现在您的 Java 类中。

通过对 XML 格式进行一些细微的修改,您的绑定就可以完美地工作。 我假设您的绑定有两个 打开标记而不是打开和关闭 是一个拼写错误,因为您说你可以在没有结构的情况下工作。 还要在绑定文件的顶部添加 。 这两个 XML mod 允许 JiBX 1.2 绑定编译器使用以下 Java 类:(

注意:您没有提供此绑定所针对的 Java 类,因此我必须根据您放入绑定文件中的信息来重建它。这样做的明显副作用是我重建了一个可以使用此绑定的类,但简单的事实是 JiBX 绑定的设计包含您需要了解的有关该类和 XML 的所有信息。)

public class TransactionRequest {
    private String version;
    private int requestCount;

    public void setVersion(String ver) {
        version = ver;
    }

    public String getVersion() {
        return version;
    }

    public void setRequestCount(int count) {
        requestCount = count;
    }

    public int getRequestCount() {
        return requestCount;
    }    
}

编译该类然后运行。绑定编译器:

>java -jar jibx-bind.jar jibx-binding.xml

为了测试它,我使用了以下sample.xml:(

注意:您也没有提供您尝试映射的XML,因此我再次根据您提供的内容创建了一个示例)

<?xml version="1.0"?>
<RequestTransaction version="0.1">
    <transHeader>
        <requestCount>3</requestCount>
    </transHeader>
</RequestTransaction>

运行测试使用以下代码:

    public static void main(String[] argz) {
        String fileName = "./sample.xml";
        IBindingFactory bfact = null;
        IUnmarshallingContext uctx = null;
        TransactionRequest sample = null;
        try {
            bfact = BindingDirectory.getFactory(TransactionRequest.class);
            uctx = bfact.createUnmarshallingContext();
            InputStream in = new FileInputStream(fileName);
            sample = (TransactionRequest)uctx.unmarshalDocument(in, null);
            System.out.println(sample.getRequestCount());
            System.out.println(sample.getVersion());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

并且运行成功。

The <structure> is arguably the most important concept in JiBX binding because it allows you to map arbitrary XML to your Java classes without forcing you to create bloated and ugly layers of nested Java objects and classes to match the XML design.

In this case your binding declares that you have an XML element named <transHeader> that will not be present in your Java class.

With some slight fixes to your XML format, your binding works perfectly. I assume the fact that your binding has two <binding> open tags rather than and open and close <binding></binding> is a typo, because you said you got it to work without the structure. Also add <?xml version="1.0"?> at the top of your binding file. Those two XML mods allow the JiBX 1.2 binding compiler to work with the following Java class:

(Note: you didn't provide the Java class this binding is for so I had to reconstruct it from the info you put in the binding file. The obvious side effect of this is that I reconstructed a class that will work with this binding. But the simple fact is that a JiBX binding by design contains all the info you need to know about the class and the XML.)

public class TransactionRequest {
    private String version;
    private int requestCount;

    public void setVersion(String ver) {
        version = ver;
    }

    public String getVersion() {
        return version;
    }

    public void setRequestCount(int count) {
        requestCount = count;
    }

    public int getRequestCount() {
        return requestCount;
    }    
}

compile the class then run the binding compiler with:

>java -jar jibx-bind.jar jibx-binding.xml

To test it I used the following sample.xml:

(Note: you also didn't provide the XML you are trying to map so again I created a sample based on what you did provide)

<?xml version="1.0"?>
<RequestTransaction version="0.1">
    <transHeader>
        <requestCount>3</requestCount>
    </transHeader>
</RequestTransaction>

Running the test uses the following code:

    public static void main(String[] argz) {
        String fileName = "./sample.xml";
        IBindingFactory bfact = null;
        IUnmarshallingContext uctx = null;
        TransactionRequest sample = null;
        try {
            bfact = BindingDirectory.getFactory(TransactionRequest.class);
            uctx = bfact.createUnmarshallingContext();
            InputStream in = new FileInputStream(fileName);
            sample = (TransactionRequest)uctx.unmarshalDocument(in, null);
            System.out.println(sample.getRequestCount());
            System.out.println(sample.getVersion());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

And it runs successfully.

醉生梦死 2024-08-02 00:20:02

已经有一段时间了,我发现这和继承有关。 我需要为继承树中的所有内容提供映射,包括我记得的接口。

我最终创建了一个包装对象,我发现这似乎是使用 JIBX 的最简单方法。 尝试映射一个真正的域类会导致卷须进入该类涉及的每个类,并且我必须解压所有内容,以便 JIBX 可以找到这些类,包括第 3 方库。

It's been a while now, but I found it was related to inheritance. I needed to give mappings for everything in the inheritance tree, including interfaces as I recall.

I ended up creating a wrapper object, which I've found seems to be the easiest way to use JIBX in general. Trying to map a true domain class causes tendrils into every class that class touches and I have to unjar everything so JIBX can find the classes, including 3rd party libs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文