Java code to XML / XSD without annotations

I need to group and ungroup Java The course doesn't belong to me. I can't add animation so that I can use JAXB

Is there a good way to convert java to XML with given constraints?

In addition, I think a tool may be helpful, but I will be more intersted. It has some Java APIs to do the same thing

Solution

Note: I am the leader of eclipse link JAXB (Moxy) and a member of JAXB (jsr-222) expert group

Domain model

I will use the following domain model for this answer Note that there are no JAXB annotations on the model

customer

package forum11693552;

import java.util.*;

public class Customer {

    private String firstName;
    private String lastName;
    private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }

    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

}

Telephone number

package forum11693552;

public class PhoneNumber {

    private String type;
    private String number;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}

Option #1 – any JAXB (jsr-222) implementation

JAXB is configured by exceptions, which means that you only need to add comments. You want the mapping behavior to be different from the default value The following is a link to an example that demonstrates how to use any JAXB impl without annotations:

demonstration

package forum11693552;

import javax.xml.bind.*;
import javax.xml.namespace.QName;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        Customer customer = new Customer();
        customer.setFirstName("Jane");
        customer.setLastName("Doe");

        PhoneNumber workPhone = new PhoneNumber();
        workPhone.setType("work");
        workPhone.setNumber("555-1111");
        customer.getPhoneNumbers().add(workPhone);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
        JAXBElement<Customer> rootElement = new JAXBElement<Customer>(new QName("customer"),Customer.class,customer);
        marshaller.marshal(rootElement,System.out);
    }

}

yield

<customer>
    <firstName>Jane</firstName>
    <lastName>Doe</lastName>
    <phoneNumbers>
        <number>555-1111</number>
        <type>work</type>
    </phoneNumbers>
</customer>

Learn more

> http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics

Option #2 – external mapping document for eclipse link JAXB (Moxy)

If you want to customize the mapping, you may be interested in Moxy's external mapping document extension The sample mapping document is as follows:

oxm. xml

<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum11693552">
    <java-types>
        <java-type name="Customer">
            <xml-root-element />
            <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>

jaxb. properties

To make Moxy your JAXB provider, you need to include a package named JAXB. In the same package as your domain model Properties with the following entries (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html ):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

demonstration

When using eclipse link Moxy as a JAXB provider (see), you can leverage external mapping documents when booting jaxbcontext

package forum11693552;

import java.util.*;
import javax.xml.bind.*;
import javax.xml.namespace.QName;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String,Object> properties = new HashMap<String,Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY,"forum11693552/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class},properties);

        Customer customer = new Customer();
        customer.setFirstName("Jane");
        customer.setLastName("Doe");

        PhoneNumber workPhone = new PhoneNumber();
        workPhone.setType("work");
        workPhone.setNumber("555-1111");
        customer.getPhoneNumbers().add(workPhone);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,System.out);
    }

}

yield

<?xml version="1.0" encoding="UTF-8"?>
<customer>
   <first-name>Jane</first-name>
   <last-name>Doe</last-name>
   <phone-number type="work">555-1111</phone-number>
</customer>

Learn more

> http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html > http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>