HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Reading and working with a large number of XML attributes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
readingnumberwithworkingxmlattributeslargeand

Problem

I'm currently facing the problem of unmarshalling a huge number (47) of attributes to a Java object. Changing the format of the XML file to be more structured is sadly not an option.

The following should serve as an example. Imagine each request element has 47 attributes instead of two.


    
    


So far I've found three possible solutions, but I'm not really happy with any of them.

Context

The XML file is refreshed minutely on the customers server and then polled and parsed by the client application. I have no influence on how it is generated or formatted.

1 Use a Bean

This is probably the most naive solution, but this gets really boilerplate heavy with more than 600 lines for 47 attributes. On the other hand, this would probably be the easiest to parse.

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

import javafx.beans.property.SimpleStringProperty;

@XmlType(name = "Request")
public class Request implements Serializable {
    private final SimpleStringProperty name = new SimpleStringProperty();

    private final SimpleStringProperty id = new SimpleStringProperty();

    public String getName() {
        return name.get();
    }

    @XmlAttribute(name = "name")
    public void setName(String name) {
        this.name.set(name);
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public String getId() {
        return id.get();
    }

    @XmlAttribute(name = "id")
    public void setId(String id) {
        this.id.set(id);
    }

    public SimpleStringProperty idProperty() {
        return id;
    }
}


2 Use a Map with String keys

A bit more manageable boilerplate-wise, but still you'd require 47 constants for all the attribute names and it leaves you inflexible if you'd need something other than a SimpleStringProperty, a SimpleDoubleProperty for example.

```
package xml.model;

import javafx.beans.property.SimpleStringP

Solution

I think the best solution is the first one because it exists some tools that are able to automatically generate classes (in bean-style) from XML.

All you need is an XML Schema so that you can use XJC (provided with Java) to let it generate these classes. Of course the generated Request class will be quite heavy with a lot of boilerplate but since it is done automatically it's not really a problem...

Going one step further

Since this class will be auto-generated, it means that you must let it untouched. Therefore it means that you can't use this class as a model (in the MVC sense) because no behaviour can be added to it.

You will have to create your own business model of a Request and this class can be much better structured. Possible example:

public final class Request {
    private final User sender
    private final User receiver;
    private final Data command;

    ...
}


With User and Data beeing other classes modeling a subpart of a request's behaviour.

What's missing now ? A bridge between your business model and the bean. This is where you can use an XmlAdapter to fill this gap. This is basically the glue between the two worlds, able to translate from the business world to the XML world and the opposite.

JAXB class XMLAdapter Business class

Code Snippets

public final class Request {
    private final User sender
    private final User receiver;
    private final Data command;

    ...
}

Context

StackExchange Code Review Q#146947, answer score: 3

Revisions (0)

No revisions yet.