My question is: are you able to CHANGE the XSD to suit your needs, or is the XSD controlled by somebody else and you MUST use it as-is?
This is important because of the way JAXB works. Basically, JAXB translates an XSD to Java classes. It can also do the reverse (translate Java classes to XSD). The relationship is described in detail here: http://docs.oracle.com/cd/E21764_01/web.1111/e13758/data_types.htm
In your case, I am assuming that somebody wrote an XSD which you used to generate Java classes, but these classes have a lot of: "Something getSomething1(), Something getSomething2(), ... Something getSomethingN() methods when you would prefer to have a List getListOfSomethings() method.
There are two ways to fix this:
(1) Change the XSD so that the "somethings" are part of a complex type that is a sequence (or anything that would cause JAXB to generate a getter for a list, as per my original answer).
This is not always possible. If the XSD is controlled by some external entity which says "this is what my data looks like, you must live with it or else your application will not be able to read my data" then you cannot do this. To give you a concrete example, suppose your application wants to read EAD data from the US library of congress. The XSD for it is here: http://www.loc.gov/ead/eadschema.html. This XSD is what it is. You cannot change it. If you change it your application will be working with YOUR definition of the data which is different. You must consider approach (2) below as you have no control over the XSD.
(2) Don't use JAXB. Instead use an XML API that allows you to query for elements. This way you can collect all "Somethings" with (for example) an XPath query (see http://docs.oracle.com/javase/tutorial/jaxp/xslt/xpath.html).
You could create a wrapper class that loads the XML and has a List getSomethings() method. This would be along these lines:
public class RequestWrapper {
Document doc;
public RequestWrapper(String xmlUri) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
this.doc = builder.parse(xmlUri);
}
public List<Something> getSomethings() {
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(<DEFINE A SUITABLE EXPRESSION>);
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
List<Something> somethings = new LinkedList<Something>();
// loop over the nodelist creating instances of Something
return somethings;
}
}
Here is a nice tutorial for using XPath with Stax which might come in handy: http://www.vogella.com/articles/JavaXML/article.html
(3) If you are willing to give up on standard Java APIs, you could consider an library that gives you more control over the bindings to Java, like Castor: http://castor.codehaus.org/xml-framework.html
Ultimately, your problem is that either the data is presented in an inconvenient manner, in which case you must do (2) or (3), or YOU have defined an inconvenient XSD, in which case you must do (1).