Take the 2-minute tour ×

I'm currently working on replacing a legacy system with JAXB and I'm running into problem with parsing the XML. The number one requirement of the system is that it must be a drop-in replacement so I cannot modify the format of the XML. Below is the XML section that is giving me trouble.


The issue with the XML is that all of the s# objects are the exact same and there can be up to 256 of them. Is there a way in JAXB to annotate such a tag or do I have to create 256 separate annotations? Any help would be most appreciated.

Here is the java code for the xx object. Note: the object was originally programmed with the understanding that there would only be 2 s# objects, but that since has changed.


public class XMLXx implements Serializable {

    private static final long serialVersionUID = 4064597372833234503L;

    private XMLSite siteOne;
    private XMLSite siteTwo;

    public XMLSite getSiteOne() {
        return siteOne;

    public void setSiteOne(XMLSite s1) {
        this.siteOne = s1;

    public XMLSite getSiteTwo() {
        return siteTwo;

    public void setSiteTwo(XMLSite s2) {
        this.siteTwo = s2;

And here is the XMLSite object:

public class XMLSite implements Serializable {

    private static final long serialVersionUID = -4374405403222014476L;

    private Integer x;
    private Integer y;

    public Integer getX() {
        return x;

    public void setX(Integer x) {
        this.x = x;

    public Integer getY() {
        return y;

    public void setY(Integer y) {
        this.y = y;
share|improve this question
256 of what? Please be specific. –  skaffman Jul 7 '11 at 19:56
of the <s#> tags. So there could be <s3> <s4> <s5> etc each with the <X> and <Y> tags as children –  Chris Flynn Jul 7 '11 at 19:57
What does your target object model look like? I lead a JAXB impl and might know a couple tricks that could help. –  Blaise Doughan Jul 7 '11 at 20:28
I'm not entirely sure of what you are asking for Blaise (I am very new to using JAXB and am a junior developer). Do you want to see the Java object I am trying to parse the XML into? –  Chris Flynn Jul 7 '11 at 20:42
Yes, that is exactly what I'm asking for. –  Blaise Doughan Jul 7 '11 at 20:44
show 2 more comments

4 Answers

up vote 5 down vote accepted

If you want to handle at the s# items as a collection:

import java.io.Serializable;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class XMLXx implements Serializable {

    private static final long serialVersionUID = 4064597372833234503L;

    private List<XMLSite> sites;

    public List<XMLSite> getSites() {
        return sites;

    public void setSites(List<XMLSite> sites) {
        this.sites = sites;


Then you could do something like to fool JAXB into thinking all the elements (s1, s2, etc) are actually called s:

import java.io.FileInputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;

public class Demo {

        JAXBContext jc = JAXBContext.newInstance(XMLXx.class);

        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("input.xml"));
        xsr = new SiteStreamReaderDelegate(xsr);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        XMLXx object = (XMLXx) unmarshaller.unmarshal(xsr);


    private static class SiteStreamReaderDelegate extends StreamReaderDelegate {

        public SiteStreamReaderDelegate(XMLStreamReader xsr) {

        public String getLocalName() {
            String localName = super.getLocalName();
            if(localName.startsWith("s")) {
                return "s";
            return localName;


For a similar example see:

share|improve this answer
Thank you so much this is exactly what I needed. –  Chris Flynn Jul 7 '11 at 21:21
add comment

Write a meta-XSD in freemarker/velocity or the like. It can define the 256 types as subtypes of some parent type using a for loop. If you want to fully automate, you can write a maven plugin to generate the XSD from the freemarker and the run generate-sources on the result.

share|improve this answer
add comment

No, I don't think so, not with standard JAXB. You could, in principle use @XmlMixed, but you'd still end up with a bunch of DOM Element objects, not bound classes. Some proprietary JAXB extension such as MOXy might be able to handle it, though.

This isn't really a good use case for JAXB. As you say, the XML is poorly designed. You'd be better off parsing this by hand (using e.g. STAX or DOM), and building the desired object model yourself.

share|improve this answer
Thank for the information, I'll have to look into parsing it by hand. –  Chris Flynn Jul 7 '11 at 20:10
add comment

JaxB does not support "dynamic" tags. Since can only be 256 of these, use a script to generate source.

share|improve this answer
add comment

Your Answer


