user1207705
user1207705

Reputation: 43

Embedded attributes in JAXB class

I need to map some legacy XML I can't change. There are several elements that have hundreds attributes exactly the same as some other elements. The attributes all have the same name postfixed with a number. So XML might look like this:

<someElement custom1="..." custom2="..." custom78=".."/>
<anotherElmenent custom1="..." custom45="..."/>

A solution that "works" is to create a base class like so:

@XmlAccessorType(FIELD)
public class LotsaCustomIds
{
    @XmlAttribute
    private String custom1;

    @XmlAttribute
    private String custom2;
 ...
}

@XmlType
public class SomeElement extends LotsaCustomIds
{
        ....
}

But it's a shame to use inheritence here, especially since Java only has single inheritence. What I'd like to do is something like the way JPA/Hibernate do embedded objects, like:

@XmlType
public class SomeElement
{
    @EmbeddedAttributes
    private LotsaCustomIds customIds;
        ....
}

Anyway to do this?

Upvotes: 1

Views: 456

Answers (1)

bdoughan
bdoughan

Reputation: 149027

Note: I'm the EclipseLink JAXB (MOXy) lead.

You could use MOXy's @XmlPath extension to map this use case. When you use it as @XmlPath(".") then it will pull the contents of the child object (LotsaCustomIds) into the parent object (SomeElement).

@XmlType
public class SomeElement
{
    @XmlPath(".")
    private LotsaCustomIds customIds;
        ....
}

Related Information from my Blog

Upvotes: 1

Related Questions