Use Jakarta Digester or JAXB?

Given the scenario: I have my own system structure for the object. Now there are several XML sources that I need to map to my Java classes. And there is no need to convert the Java object to XML.

How do you suggest me to use Digester or JAXB? I currently tend to use Digester because I can specify an XML path for each XML source to the same object method call, and Digester seems to be easier to maintain. Although JAXB has a good design for marshaling / demonstrating java and XML, but I find it too complicated, every xml-java mapping requires an xml-schema, right?

I think both Digester or JAXB have their own mission to fit different usage scenarios, so you need your advice to help me solve one of them. Many thanks.

+3
source share
3 answers

The advantage of JAXB is that it is a specification (JSR-222) with several implementations: Metro , EclipseLink MOXy , JaxMe . This avoids the problem of blocking the provider.

XPath-based mapping

EclipseLink JAXB (MOXy) has an extension to support XPath-based mapping (I am the technical leader).

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

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>

:

+3

, JAXB. , , . , XML- .

, - . JAXB java- XML-, , . , . , .

Digester, , ( ), , .

+4

Maybe off topic: I abandoned the digester in favor of xstream. Maybe look

+1
source

Source: https://habr.com/ru/post/1784039/


All Articles