Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7777417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:08:12+00:00 2026-06-01T18:08:12+00:00

I am trying to map the following XML structure within a larger document, clearly

  • 0

I am trying to map the following XML structure within a larger document, clearly this isn’t the greatest use of XML ever but this is what I have to work with.

Example simplified for clarity:

  <details>
    <pictures>
      <picture1>
        http://domain.com/path/picture1.jpg
      </picture1>
      <picture2>
        http://domain.com/path/picture2.jpg
      </picture2>
      <picture3>
        http://domain.com/path/picture3.jpg
      </picture3>
      <picture4>
        http://domain.com/path/picture4.jpg
      </picture4>
      <picture5>
        http://domain.com/path/picture5.jpg
      </picture5>
      <picture6>
        http://domain.com/path/picture6.jpg
      </picture6>
      <picture7>
        http://domain.com/path/picture7.jpg
      </picture7>
    </pictures>
  </details>

There is a DTD for this document which declares that there will be up to 30 different picture elements numbered 1-30 with the names <picutre[n]/>

What I would like to do is, rather than create 30 different classes for each of these elements called Picture1, Picture2, Picture3 … and so on. I would just like to use a single Picture class and use it for all 30 unique element names.

Below is what I have tried so far.

@XmlRootElement
public class Details {

   ...

   @XmlElementWrapper
   @XmlElementRefs({
        @XmlElementRef( name="picture1", type=Picture.class ),
        @XmlElementRef( name="picture2", type=Picture.class ),
        @XmlElementRef( name="picture3", type=Picture.class ),
        @XmlElementRef( name="picture4", type=Picture.class ),
        @XmlElementRef( name="picture5", type=Picture.class ),
        @XmlElementRef( name="picture6", type=Picture.class ),
        @XmlElementRef( name="picture7", type=Picture.class ),
        @XmlElementRef( name="picture8", type=Picture.class ),
        @XmlElementRef( name="picture9", type=Picture.class ),
        @XmlElementRef( name="picture10", type=Picture.class ),
        @XmlElementRef( name="picture11", type=Picture.class ),
        @XmlElementRef( name="picture12", type=Picture.class ),
        @XmlElementRef( name="picture13", type=Picture.class ),
        @XmlElementRef( name="picture14", type=Picture.class ),
        @XmlElementRef( name="picture15", type=Picture.class ),
        @XmlElementRef( name="picture16", type=Picture.class ),
        @XmlElementRef( name="picture17", type=Picture.class ),
        @XmlElementRef( name="picture18", type=Picture.class ),
        @XmlElementRef( name="picture19", type=Picture.class ),
        @XmlElementRef( name="picture20", type=Picture.class ),
        @XmlElementRef( name="picture21", type=Picture.class ),
        @XmlElementRef( name="picture22", type=Picture.class ),
        @XmlElementRef( name="picture23", type=Picture.class ),
        @XmlElementRef( name="picture24", type=Picture.class ),
        @XmlElementRef( name="picture25", type=Picture.class ),
        @XmlElementRef( name="picture26", type=Picture.class ),
        @XmlElementRef( name="picture27", type=Picture.class ),
        @XmlElementRef( name="picture28", type=Picture.class ),
        @XmlElementRef( name="picture29", type=Picture.class ),
        @XmlElementRef( name="picture30", type=Picture.class )
    })
   public List<Picture> getPictures() {
      return this.pictures;
   }

   public void setPictures( List<Pictures> pictures ) {
      this.pictures = pictures;
   }

   ...

}

@XmlElementWrapper
public class Picture {
 ...
}

This causes getPictures to return null always.

Additionally I tried changing the annotation on the Picture class to be @XmlElementWrapper( name="picture1" ) which resulted in me getting a list returned from getPictures() but only ever containing the <picture1/> element and never the rest.

I know I can resort to getting a list of JAXBElement objects instead but I would rather not if I can avoid it. Any ideas how I can map this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:08:14+00:00Added an answer on June 1, 2026 at 6:08 pm

    There are a couple ways you could handle this use case:

    Option #1

    You could do the following by leveraging @XmlElements:

    package forum10109418;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Details {
    
       private List<Picture> pictures;
    
       @XmlElementWrapper
       @XmlElements({
           @XmlElement( name="picture1", type=Picture.class ),
           @XmlElement( name="picture2", type=Picture.class ),
           @XmlElement( name="picture3", type=Picture.class ),
           @XmlElement( name="picture4", type=Picture.class ),
           @XmlElement( name="picture5", type=Picture.class ),
           @XmlElement( name="picture6", type=Picture.class ),
           @XmlElement( name="picture7", type=Picture.class ),
           @XmlElement( name="picture8", type=Picture.class ),
           @XmlElement( name="picture9", type=Picture.class ),
           @XmlElement( name="picture10", type=Picture.class ),
           @XmlElement( name="picture11", type=Picture.class ),
           @XmlElement( name="picture12", type=Picture.class ),
           @XmlElement( name="picture13", type=Picture.class ),
           @XmlElement( name="picture14", type=Picture.class ),
           @XmlElement( name="picture15", type=Picture.class ),
           @XmlElement( name="picture16", type=Picture.class ),
           @XmlElement( name="picture17", type=Picture.class ),
           @XmlElement( name="picture18", type=Picture.class ),
           @XmlElement( name="picture19", type=Picture.class ),
           @XmlElement( name="picture20", type=Picture.class ),
           @XmlElement( name="picture21", type=Picture.class ),
           @XmlElement( name="picture22", type=Picture.class ),
           @XmlElement( name="picture23", type=Picture.class ),
           @XmlElement( name="picture24", type=Picture.class ),
           @XmlElement( name="picture25", type=Picture.class ),
           @XmlElement( name="picture26", type=Picture.class ),
           @XmlElement( name="picture27", type=Picture.class ),
           @XmlElement( name="picture28", type=Picture.class ),
           @XmlElement( name="picture29", type=Picture.class ),
           @XmlElement( name="picture30", type=Picture.class )
       })
    
       public List<Picture> getPictures() {
          return this.pictures;
       }
    
       public void setPictures( List<Picture> pictures ) {
          this.pictures = pictures;
       }
    
    }
    

    Option #2

    You could map your Details class as follows:

    package forum10109418;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Details {
    
       private List<Picture> pictures;
    
       @XmlElementWrapper
       @XmlElement(name="picture")
       public List<Picture> getPictures() {
          return this.pictures;
       }
    
       public void setPictures( List<Picture> pictures ) {
          this.pictures = pictures;
       }
    
    }
    

    And then use a StreamReaderDelegate to chop off the numeric suffix:

    package forum10109418;
    
    import java.io.FileInputStream;
    import javax.xml.bind.*;
    import javax.xml.stream.*;
    import javax.xml.stream.util.StreamReaderDelegate;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Details.class);
    
            XMLInputFactory xif = XMLInputFactory.newFactory();
            XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("src/forum10109418/input.xml"));
            xsr = new StreamReaderDelegate(xsr) {
                @Override
                public String getLocalName() {
                    String localName = super.getLocalName();
                    if(localName.startsWith("picture") && !localName.equals("pictures")) {
                        return "picture";
                    }
                    return localName;
                }
    
            };
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Details details = (Details) unmarshaller.unmarshal(xsr);
            System.out.println(details.getPictures().size());
        }
    
    }
    

    Option #3

    If you are using EclipseLink MOXy as your JAXB (JSR-222) provider, then you could use the @XmlVariableNode extension that we added:

    • http://blog.bdoughan.com/2013/06/mapping-bad-xml-enumerated-collection.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this isn't RESTful, but for now, I'm trying to set up an
I am trying to map a java model into some XML. To do this
Trying to map the following schema using the Entity Framework. A Customer can have
I'm trying to map a Dictionary containing Lists. I have the following set of
I've been trying to map an object to a database using SQLAlchemy but have
I have an XML file in the following structure. <NewDataSet> <markers> <name>name text</name> <desc>desc
I'm trying to map the following model with Castle ActiveRecord Contact (a person represented
I am trying to connect Hibernate(3.2.5) with PostgreSQL 8.2., but I am getting following
I am trying to set transparent panel using this tutorial in my app but
I'm trying to map a Controller with Spring 3.0 without success. I've the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.