Case Insensitive Jaxb2Marshaller

时间:2015-11-27 12:49:47

标签: java xml jaxb spring-batch case-insensitive

Jaxb2Marshaller org.springframework.oxm.jaxb.Jaxb2Marshaller)是Spring的O / X Mapping集成类的一部分。

我将它用作StaxEventItemReader

的解组合
<bean class="org.springframework.batch.item.xml.StaxEventItemReader">
    <property name="marshaller">
        <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>xx.xx.xx.MyBean1</value>
                    <value>xx.xx.xx.MyBean2</value>
                    <value>xx.xx.xx.MyBean3</value>
                </list>
            </property>
        </bean>
    </property>
</bean>

MyBeanX个类使用javax.xml.bind.annotation注释进行注释:

@XmlRootElement(name="MyBean1")
@XmlType(propOrder = {"MyBean2", "MyBean2"})
public class MyBean1 implements Serializable {

    private MyBean2 myBean2;
    private MyBean3 myBean3;

    @XmlElement(name="MyBean2")
    public MyBean2 getMyBean2() {
        return myBean2;
    }

    [...]
}

不幸的是,我需要解组的XML可以区分它们:

<MYBEAN1>
    <MYBEAN2></MYBEAN2>
    <MYBEAN3></MYBEAN3>
</MYBEAN1>    

<MyBean1>
    <MyBean2></MyBean2>
    <MyBean3></MyBean3>
</MyBean1>

<mybean1>
    <mybean2></mybean2>
    <mybean3></mybean3>
</mybean1>

或更多变种......

现在,我需要能够自觉地解析这些XML案例。到目前为止我看到的可能是创建StreamReaderDelegate并通过XMLStreamReader传递标签为小写或大写。

XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("file.xml"));
xsr = new MyStreamReaderDelegate(xsr);

MyStreamReaderDelegate看起来像这样:

private static class MyStreamReaderDelegate extends StreamReaderDelegate {

    public MyStreamReaderDelegate(XMLStreamReader xsr) {
        super(xsr);
    }

    @Override
    public String getAttributeLocalName(int index) {
        return super.getAttributeLocalName(index).toLowerCase();
    }

    @Override
    public String getLocalName() {
        return super.getLocalName().toLowerCase();
    }

}

我的问题是我不知道用什么方法来覆盖(以及在哪个类中)通过这个委托传递我的XML。查看Jax2Marshaller源代码,我发现方法XMLStreamReader中使用了unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource)

private Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource) throws JAXBException {
    XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(staxSource);
    if (streamReader != null) {
        return jaxbUnmarshaller.unmarshal(streamReader);
    }
    else {
        XMLEventReader eventReader = StaxUtils.getXMLEventReader(staxSource);
        if (eventReader != null) {
            return jaxbUnmarshaller.unmarshal(eventReader);
        }
        else {
            throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader");
        }
    }
}

所以我的问题是,如何覆盖此方法以添加委托?

1 个答案:

答案 0 :(得分:1)

您可以使用版本2.6+的Moxy来实现不区分大小写的编组。对于为特定目的而进行的特定增强,见link

您必须将UNMARSHALLING_CASE_INSENSITIVE属性设置为true。下面是一些示例代码

final StaxEventItemReader<ProductDTO> itemReader = new StaxEventItemReader<>();
final Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
final Map<String, Boolean> properties = Maps.newHashMap();
properties.put(UnmarshallerProperties.UNMARSHALLING_CASE_INSENSITIVE, Boolean.TRUE);
unMarshaller.setUnmarshallerProperties(properties);
itemReader.setUnmarshaller(unMarshaller);