困难的JAXB映射

时间:2012-04-11 15:47:23

标签: java jaxb

我正在尝试在更大的文档中映射以下XML结构,显然这不是XML的最大用途,但这是我必须使用的。

为简化起见,简化了示例:

  <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>

此文档有一个DTD,声明最多可有30个不同的图片元素,编号为1-30,名称为<picutre[n]/>

我想做的是,而不是为这些元素创建30个不同的类,分别称为Picture1,Picture2,Picture3 ......等等。我只想使用一个Picture类并将其用于所有30个唯一元素名称。

以下是我到目前为止所尝试的内容。

@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 {
 ...
}

这会导致getPictures始终返回null。

此外,我尝试将Picture类的注释更改为@XmlElementWrapper( name="picture1" ),这导致我从getPictures()返回一个列表,但只包含<picture1/>元素,而不是其余元素。

我知道我可以求助于获取JAXBElement对象列表,但如果可以避免它,我宁愿不这样做。我有什么想法可以映射这个吗?

1 个答案:

答案 0 :(得分:5)

有几种方法可以处理这个用例:

选项#1

您可以通过利用@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;
   }

}

选项#2

您可以按如下方式映射Details课程:

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;
   }

}

然后使用StreamReaderDelegate来删除数字后缀:

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());
    }

}

选项#3

如果您使用EclipseLink MOXy作为JAXB (JSR-222)提供商,则可以使用我们添加的@XmlVariableNode扩展程序: