khawarizmi
khawarizmi

Reputation: 732

Dynamic XML attributes with JAXB

I have an XML file that I am trying to unmarshall to java object using JAXB

<root>
    <object att1="orgA" att2="orgA" id="6" name="">
        ...
    </object>
</root>

Java class:

@AllArgsConstructor
@NoArgsConstructor
@ToString
@Setter
@XmlRootElement(name="object")
public class ObjectElement implements Serializable {

@XmlAttribute
private int id;

@XmlAttribute
private String attr1;

@XmlAttribute
private String attr2;

@XmlAttribute
private String name;

}

Question: attributes of xml element object may change dynamically. So keys of the attributes may be dynamic so there is no way of putting them as XmlAttributes in class. Is there a way to define some sort of HashMap that reads all keys of object and corresponding values? For instance new object could have totally different attributes like this;

<object att5="some" att7="other" id="6" name="value">
    ...
</object>

Upvotes: 1

Views: 2599

Answers (1)

Michał Ziober
Michał Ziober

Reputation: 38625

You need to write custom adapter which extends javax.xml.bind.annotation.adapters.XmlAdapter class. In your case, we can create a Map which represents all properties. After reading properties you can set all POJO fields using reflection on or manually. Custom deserialiser which reads all attributes dynamically could look like below:

class ItemXmlAdapter extends XmlAdapter<Object, Item> {

    @Override
    public Item unmarshal(Object v) {
        Element element = (Element) v;

        Map<String, String> properties = new HashMap<>();
        NamedNodeMap attributes = element.getAttributes();
        for (int i = attributes.getLength() - 1; i >= 0; i--) {
            Node node = attributes.item(i);
            properties.put(node.getNodeName(), node.getNodeValue());
        }

        Item item = new Item();
        item.setProperties(properties);

        return item;
    }

    @Override
    public Object marshal(Item v) throws Exception {
        return null; // Implement if needed
    }
}

Simple example app which reads XML and parses all attributes:

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JaxbApp {

    public static void main(String[] args) throws Exception {
        File xmlFile = new File("./resource/test.xml").getAbsoluteFile();

        JAXBContext context = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = context.createUnmarshaller();
        Root root = (Root) unmarshaller.unmarshal(xmlFile);

        System.out.println(root);
    }
}

@XmlRootElement(name = "root")
class Root {

    private List<Item> items = new ArrayList<>();

    @XmlElement(name = "object")
    public List<Item> getItems() {
        return items;
    }

    // getters, setters, toString
}

@XmlJavaTypeAdapter(ItemXmlAdapter.class)
class Item {

    private Map<String, String> properties;

    // getters, setters, toString
}

For below XML:

<root>
    <object att1="orgA" att2="orgA" id="6" name="N"/>
    <object att5="some" id="6" name="value"/>
</root>

Prints:

Items{items=[{name=N, id=6, att2=orgA, att1=orgA}, {name=value, att5=some, id=6}]}

Upvotes: 2

Related Questions