使用 Jakarta Digester 还是 JAXB?

发布于 2024-10-11 03:50:26 字数 324 浏览 7 评论 0原文

给定一个场景: 我有自己的系统对象结构。现在,我必须将多个 XML 源映射到我的 java 类。并且无需将 Java 对象转换回 XML。

您对我使用 Digester 或 JAXB 有什么建议?目前我倾向于使用 Digester,因为我可以为每个 XML 源指定 XML 路径到同一个对象方法调用,而且 Digester 似乎更容易维护。虽然JAXB对marshal/unmarshal java和XML有很好的设计,但我认为它太复杂了,每个xml-java映射都需要xml模式,对吧?

我认为 Digester 或 JAXB 都有适合不同使用场景的使命,因此需要您的建议来帮助我决定其中之一。多谢。

Given a scenario:
I have my own system's object structure. Now there are more than one XML sources I have to map to my java classes. And there is no need to convert Java object back into XML.

What's your suggestion for me to use Digester or JAXB? Currently I lean to use Digester, because I can specify XML path for each XML source to the same object method call, and Digester seems to be easier to maintain. Although JAXB has good design to marshal/unmarshal java and XML, but I think it is too complicated, xml schema is needed for each xml-java mapping, right?

I think both Digester or JAXB has their mission to fit different usage scenario, so need your advice to help me decide one of them. Thanks a lot.

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

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

发布评论

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

评论(3

淡莣 2024-10-18 03:50:26

我认为您可能对 JAXB 有偏见。如果您选择这样做,它可以变得复杂,但也可以非常简单。例如,您可以仅使用一个注释将整个 XML 文档绑定到对象图上。

另外,模式的事情是一个转移注意力的事情。 JAXB可以从 XML 模式生成 Java 代码,但这只是在您拥有模式的情况下的一种方便。如果不这样做,请忽略该部分。您可以手动注释您的类模型,这非常简单。

另一方面,消化器更难维护(在我看来),因为您必须在类模型中添加路径表达式。

I think you may have a skewed view of JAXB. It can be complicated, if you choose to make it so, but it can also be extremely simple. For example, you can bind an entire XML document onto an object graph with only a single annotation.

Also, the schema thing is a red herring. JAXB can generate java code from an XML Schema, but that's just a convenience for cases where you have a schema. If you don't, then ignore that part. You can annotate your class model by hand, it's very easy.

Digester, on the other hand, is harder to maintain (in my opinion), since you have to muck about with path expressions ion addition to your class model.

×眷恋的温暖 2024-10-18 03:50:26

JAXB 的一个优点是它是一个具有多种实现的规范 (JSR-222):MetroEclipseLink MOXyJaxMe。这避免了供应商锁定的问题。

基于 XPath 的映射

EclipseLink JAXB ( MOXy) 有一个扩展来支持基于 XPath 的映射(我是技术主管)。

package blog.geocode;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="kml")
@XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
public class Address {

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
    private String street;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
    private String city;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
    private String state;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
    private String country;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
    private String postalCode;

}

多个 XML 源

要将多个 XML 表示形式应用于对象模型,您可以利用 MOXy 的 XML 元数据。这是 JAXB 标准的另一个扩展。示例文件如下所示:

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="blog.bindingfile">
    <xml-schema
        namespace="http://www.example.com/customer"
        element-form-default="QUALIFIED"/>
    <java-types>
        <java-type name="Customer">
            <xml-root-element/>
            <xml-type prop-order="firstName lastName address phoneNumbers"/>
            <java-attributes>
                <xml-element java-attribute="firstName" name="first-name"/>
                <xml-element java-attribute="lastName" name="last-name"/>
                <xml-element java-attribute="phoneNumbers" name="phone-number"/>
            </java-attributes>
        </java-type>
        <java-type name="PhoneNumber">
            <java-attributes>
                <xml-attribute java-attribute="type"/>
                <xml-value java-attribute="number"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

有关详细信息:

An advantage of JAXB is that it is a spec (JSR-222) with multiple implementations: Metro, EclipseLink MOXy, JaxMe. This avoids the problem of vendor lock in.

XPath Based Mapping

The EclipseLink JAXB (MOXy) has an extension to support XPath based mapping (I'm the tech lead).

package blog.geocode;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="kml")
@XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
public class Address {

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
    private String street;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
    private String city;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
    private String state;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
    private String country;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
    private String postalCode;

}

Multiple XML Sources

To apply multiple XML representations to an object model you can leverage MOXy's XML metadata. This is another extension to the JAXB standard. An example file looks like:

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="blog.bindingfile">
    <xml-schema
        namespace="http://www.example.com/customer"
        element-form-default="QUALIFIED"/>
    <java-types>
        <java-type name="Customer">
            <xml-root-element/>
            <xml-type prop-order="firstName lastName address phoneNumbers"/>
            <java-attributes>
                <xml-element java-attribute="firstName" name="first-name"/>
                <xml-element java-attribute="lastName" name="last-name"/>
                <xml-element java-attribute="phoneNumbers" name="phone-number"/>
            </java-attributes>
        </java-type>
        <java-type name="PhoneNumber">
            <java-attributes>
                <xml-attribute java-attribute="type"/>
                <xml-value java-attribute="number"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

For more information:

被你宠の有点坏 2024-10-18 03:50:26

也许偏离主题:我已经放弃了digester,转而使用xstream。也许看看

Maybe off topic: I've abandonded digester in favor of xstream. Maybe have a look

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