【问题标题】:XPath 1.0 queries on JAXB objects?XPath 1.0 对 JAXB 对象的查询?
【发布时间】:2010-06-22 08:13:02
【问题描述】:

JAXB 非常棒,可以真正节省时间,但遍历生成的对象树仍然非常耗时;几乎和直接使用 DOM 一样糟糕。

有没有一种方法可以让我在 JAXBElement 上执行 XPath 1.0 查询,而不必每次都煞费苦心地将文档编组到 DOM 模型和从 DOM 模型输出?

【问题讨论】:

    标签: java xpath jaxb


    【解决方案1】:

    不直接,不。但是,您可以使用Apache Commons Jxpath,它允许您跨任意对象图运行 XPath 查询,而不仅仅是 JAXB 绑定的对象图。它可以在“宽松”模式下运行,可以容忍空值。

    非常方便替换那些容易发生 NPE 的图形导航。

    【讨论】:

      【解决方案2】:

      接受的答案来自 2010 年,这篇文章是为了帮助其他希望将 XPath 与 JAXB 一起使用的人。 Moxy 实现提供了许多不错的扩展,其中之一就是执行 XPath。在Moxy's tutorial 上阅读更多相关信息。从同一个地方复制的示例

      Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
      ...
      int customerId = jaxbContext.getValueByXPath(customer, "@id", null, Integer.class);
      jaxbContext.setValueByXPath(customer, "first-name/text()", null, "Bob");
      jaxbContext.setValueByXPath(customer, "phone-number/area-code/text()", null, "555");
      ...
      jaxbContext.createMarshaller().marshal(customer, System.out);
      

      【讨论】:

        猜你喜欢
        • 2018-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 1970-01-01
        • 2014-03-16
        相关资源
        最近更新 更多