如何调试 JAXB 解组?

发布于 2024-12-25 13:12:12 字数 812 浏览 2 评论 0原文

我遇到了 JAXB 解组问题。我认为我已经正确编码了它,但是我的未编组对象返回了空参数。因此,我假设在解组时,JAXB 没有看到它所期望的适当的 XML 结构。但是,我没有收到任何错误消息或抛出任何异常。

无论如何,是否可以逐步完成解组过程,以准确了解无法填充我的对象的位置/原因?

实际的解组代码相当普通:

public <T> T unmarshall(Node node, Class<T> clazz) throws JAXBException {
    // Creating an unmarshaller
    Unmarshaller u = JAXBContext.newInstance(clazz).createUnmarshaller();

    // unmarshal an instance node into  Java content
    return clazz.cast(u.unmarshal(node, clazz).getValue());
}

但是,当我调用它时,我得到一个返回的 clazz 类型的对象(如预期),但未填充。

我尝试解组的 DOM 对象是由第三方 API 生成的。我已经在解组过程中遇到了一些极其奇怪的行为,这就是为什么我希望能够调试该过程。例如,如果我尝试解组 DOM 对象中的子元素(即:doc.getByElementName("myElement").item(0)),它会默默地失败。但是,如果我将文档转换为字符串,并将其重新导入到新文档中,则它可以很好地转换。

我开始感到非常沮丧,不知道如何调试这个问题。

感谢您的任何见解!

埃里克

I'm running into a problem with JAXB unmarshalling. I think I have it properly coded, but my unmarshalled object returns with null parameters. Consequently, I am assuming that when unmarshalling, JAXB is not seeing the appropriate XML structure that it is expecting. However, I do not get any error messages or any exceptions thrown.

Is there anyway to step through the unmarshalling process to see exactly where/why it is failing to populate my object(s)?

The actual unmarshalling code is fairly mundane:

public <T> T unmarshall(Node node, Class<T> clazz) throws JAXBException {
    // Creating an unmarshaller
    Unmarshaller u = JAXBContext.newInstance(clazz).createUnmarshaller();

    // unmarshal an instance node into  Java content
    return clazz.cast(u.unmarshal(node, clazz).getValue());
}

However, when I call it, I get an object of type clazz returned (as expected), but unpopulated.

The DOM object that I am trying to unmarshal is generated by a third party API. I have already run into some extremely odd behaviours with the unmarshalling, which is why I would like to be able to debug the process. For instance, if I try to unmarshal a sub-element within the DOM object (ie: doc.getByElementName("myElement").item(0)), it fails silently. However, if I convert the document to a string, and reimport it into a new document, then it converts it fine.

I am starting to get quite frustrated not knowing how to debug this issue.

Thanks for any insights!

Eric

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

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

发布评论

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

评论(3

蹲墙角沉默 2025-01-01 13:12:12

您可以尝试使用默认处理程序。

JAXBContext context = JAXBContext.newInstance(jaxbObjectClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());

来自javadoc:

公共类DefaultValidationEventHandler

扩展对象

实现ValidationEventHandler

JAXB 1.0 仅默认验证事件处理程序。这是从 JAXBContext 创建的所有对象的默认处理程序,该 JAXBContext 管理由 JAXB 1.0 绑定编译器生成的模式派生代码。此处理程序会导致解组和验证操作在出现第一个错误或致命错误时失败。

这将产生更详细、更有用的调试输出。

You can try using the default handler.

JAXBContext context = JAXBContext.newInstance(jaxbObjectClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());

From the javadocs:

public class DefaultValidationEventHandler

extends Object

implements ValidationEventHandler

JAXB 1.0 only default validation event handler. This is the default handler for all objects created from a JAXBContext that is managing schema-derived code generated by a JAXB 1.0 binding compiler. This handler causes the unmarshal and validate operations to fail on the first error or fatal error.

This will produce more verbose, useful output for debugging.

§普罗旺斯的薰衣草 2025-01-01 13:12:12

您可以采用的一种方法是使用 JAXB 从带注释的类生成 XML 模式。这表示 JAXB 期望输入文档的外观。然后根据此 XML 模式验证您的 XML 文档,看看它是否符合 JAXB 的期望。

One approach you could take is to use JAXB to generate an XML schema from your annotated classes. This represents what JAXB expects the input document to look like. Then validate your XML document against this XML schema to see if it conforms to JAXB's expectations.

§普罗旺斯的薰衣草 2025-01-01 13:12:12

如果其他人像我一样偶然发现这一点。 https://www.kevinhooke.com/2014/03 /25/debugging-jaxb-unmarshalling-issues/

这篇博文至关重要

将以下内容传递到您的启动命令中

-Djaxb.debug=true

然后添加以下 EventHandler。

Unmarshaller um = jaxbContext.createUnmarshaller();
um.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());

希望有帮助。在我的 jetty Web 应用程序中,这会导致明显的错误出现,并且我很容易发现命名空间问题。

Should anyone else stumble on this like I did. https://www.kevinhooke.com/2014/03/25/debugging-jaxb-unmarshalling-issues/

This blog post was essential

Pass the following into your start command

-Djaxb.debug=true

Then add the following EventHandler.

Unmarshaller um = jaxbContext.createUnmarshaller();
um.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());

hope that helps. In my jetty web app, this enables clear errors to bubble up and I found the namespace issue quite easily.

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