Excellent XML mapping / binding to the same Java object

I have a Java application compatible with several other information systems

The same object can be displayed in different XML files in accordance with the target system

My question is: is there a Java solution for doing server-side XML mapping / binding on the same object

Something similar to Bean Validation groups that allow you to validate an object with a difference validation profile

In JAXB style, it could be something like this, for example:

// pseudocode
@XmlRootElement(name="person", , profile="profile1")
@XmlRootElement(name="individual", profile="profile2")
@XmlRootElement(name="human", profile="profile3")
public class Person {

    @XmlElement(name = "lastName", profile="profile1")
    @XmlElement(name = "surname", profile="profile2")
    @XmlElement(name = "famillyName", profile="profile3")
    private String lastName;

    //...
}

and then

    // pseudocode
    File file = new File("C:\\file.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller("profile1");
    jaxbMarshaller.marshal(person, file);

    //...

    File file = new File("C:\\file.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller("profile1");
    Person person = (Person) jaxbUnmarshaller.unmarshal(file);

Is it possible to do this with JAXB, but I won't recognize


Edit

@Martin Serrano's answer gives me some tips for optimizing things with JAXB.

An abstract class to manage all of them:

@XmlTransient
public abstract class APerson {

    protected String lastname;

    public APerson() {
    }

    public APerson(APerson p) {
        lastname = p.lastname;
    }

    public abstract String getLastname();

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + "{" + lastname + "}";
    }
}

And it specifies the classes that perform the mapping:

Person :

@XmlRootElement
public class Person extends APerson {

    public Person() {
    }

    public Person(APerson p) {
        super(p);
    }

    @Override
    public String getLastname() {
        return lastname;
    }
}

:

@XmlRootElement
public class Human extends APerson {

    public Human() {
    }

    public Human(APerson p) {
        super(p);
    }

    @Override
    @XmlElement(name = "famillyName")
    public String getLastname() {
        return lastname;
    }
}


@XmlRootElement
public class Individual extends APerson{   

    public Individual() {
    }

    public Individual(APerson p) {
        super(p);
    }    

    @Override
    @XmlElement(name = "surname")
    public String getLastname() {
        return lastname;
    }
}

:

public class Main {

    public static void main(String[] args) throws Exception {
        Person person = new Person();
        person.setLastname("Doe");

        String fileName = "person.xml";
        marshal(person, fileName);
        person = unmarshal(Person.class, fileName);
        System.out.println(person);

        fileName = "human.xml";
        Human human = new Human(person);
        marshal(human, fileName);
        human = unmarshal(Human.class, fileName);
        System.out.println(human);
        person = new Person(human);
        System.out.println(person);

        fileName = "individual.xml";
        Individual individual = new Individual(person);
        marshal(individual, fileName);
        individual = unmarshal(Individual.class, fileName);
        System.out.println(individual);
        person = new Person(individual);
        System.out.println(person);
    }

    private static <P extends APerson> void marshal(P person, String fileName) throws JAXBException {
        File file = new File(fileName);
        JAXBContext context = JAXBContext.newInstance(person.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(person, file);
        marshaller.marshal(person, System.out);
    }

    private static <P extends APerson> P unmarshal(Class<P> cl, String fileName) throws JAXBException {
        File file = new File(fileName);
        JAXBContext context = JAXBContext.newInstance(cl);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        P person = (P) unmarshaller.unmarshal(file);
        return person;
    }

}

:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
    <lastname>Doe</lastname>
</person>
Person{Doe}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
    <famillyName>Doe</famillyName>
</human>
Human{Doe}
Person{Doe}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<individual>
    <surname>Doe</surname>
</individual>
Individual{Doe}
Person{Doe}

, , , .

, !

, disinct, 3 ...

+4
1

- - . JAXB - . . Person , . , jaxb .

, .

, :

@XmlRootElement(name="person")
public interface Person {
  @XmlElement(name = "lastName")
  private String lastName;
}

System1 :

@XmlRootElement(name="individual")
public class Individual implements Person {
}

:

@XmlRootElement(name="human")
public class Human implements Person {
}
0

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


All Articles