JAXB与动态元素的编组 [英] JAXB Unmarshalling with dynamic elements

查看:65
本文介绍了JAXB与动态元素的编组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面有一个XML.

 <hotelRoomDetails>
  <NightRates>
   <Night1>67</Night1>
   <Night2>67.5</Night2> 
   ........
   ........
   <Night25>65</Night25> 
  </NightRates>
  .......
  </hotelRoomDetails>

元素Night1,Night2,Night3始终是动态的,并且此计数可能在1到35倍之间变化.父标签<NightRates>始终是一致的.

The element Night1,Night2,Night3 are always dynamic, and this count may vary from 1 to 35 times. Parent tag <NightRates> is always consistent.

元素<Night1><Night2> ..不具有任何其他属性.它提供有关每晚酒店房价的信息.

The element <Night1>, <Night2> .. are not having any other attribute. It gives information about the hotel rate per night.

我想创建一个"LinkedList"(保留订单),其中包含各个夜晚的房价信息.在不知道元素出现次数的情况下如何处理这种情况?如何为此XML创建Java类?

I want to create a 'LinkedList'(to preserve order) which contains the rate information of individual night. How can I handle this situation without knowing the occurrence count of element? How to create java class for this xml?

推荐答案

仅解组

如果您只是在进行编组,则可以使用StreamReaderDelegate去除数字后缀.

演示

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.stream.util.StreamReaderDelegate;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(HotelRoomDetails.class);

        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("src/forum19834756/input.xml"));
        xsr = new StreamReaderDelegate(xsr) {
            @Override
            public String getLocalName() {
                String localName = super.getLocalName();
                if(localName.startsWith("Night") && !localName.equals("NightRates")) {
                    return "Night";
                }
                return localName;
            }

        };

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        HotelRoomDetails details = (HotelRoomDetails) unmarshaller.unmarshal(xsr);
        System.out.println(details.getNightRates().size());
    }

}

HotelRoomDetails

然后您有一个collection属性,该属性映射到名为Night的元素.

Then you have a collection property that maps to the element called Night.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class HotelRoomDetails {

    private List<String> nightRates = new ArrayList<String>();

    @XmlElementWrapper(name = "NightRates")
    @XmlElement(name = "Night")
    public List<String> getNightRates() {
        return nightRates;
    }

}


元帅&元帅

注意::我是 EclipseLink JAXB(MOXy ) 的负责人和 JAXB(JSR -222) 专家组.


Unmarshal & Marshal

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

如果您需要支持对此格式的读写,则可以使用MOXy的@XmlVariableNode扩展名.

If you need to support reading and writing to this format you could use MOXy's @XmlVariableNode extension.

这篇关于JAXB与动态元素的编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆