[英]Case Insensitive Jaxb2Marshaller
Jaxb2Marshaller ( org.springframework.oxm.jaxb.Jaxb2Marshaller ) is part of Spring's O/X Mapping integration classes. Jaxb2Marshaller ( org.springframework.oxm.jaxb.Jaxb2Marshaller )是Spring的O / X Mapping集成类的一部分。
I use it as the unmarshaller of a StaxEventItemReader
: 我将其用作
StaxEventItemReader
的解组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
classes are annoted with javax.xml.bind.annotation
annotations : 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;
}
[...]
}
Unfortunately, the XMLs I need to unmarshall can have the case difference between them : 不幸的是,我需要解组的XML可能在大小写上有所不同:
<MYBEAN1>
<MYBEAN2></MYBEAN2>
<MYBEAN3></MYBEAN3>
</MYBEAN1>
<MyBean1>
<MyBean2></MyBean2>
<MyBean3></MyBean3>
</MyBean1>
<mybean1>
<mybean2></mybean2>
<mybean3></mybean3>
</mybean1>
or more variants... 或更多变体...
Now, I need to be able to parse those XML case-insentively. 现在,我需要能够实例分析这些XML。 What I saw so far was possible is to create a
StreamReaderDelegate
and pass the XMLStreamReader
through it to either convert tags to lowercase or uppercase. 到目前为止,我所看到的是创建一个
StreamReaderDelegate
并将XMLStreamReader
传递给它,以将标签转换为小写或大写形式。
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("file.xml"));
xsr = new MyStreamReaderDelegate(xsr);
Where MyStreamReaderDelegate
looks like this : 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();
}
}
My problem is I don't know what method to override (and in which class) to pass my XML through this delegate. 我的问题是我不知道要重写哪种方法(以及在哪个类中)以通过此委托传递XML。 Looking at
Jax2Marshaller
source code, I found out that an XMLStreamReader
is used in the method unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource)
: 查看
Jax2Marshaller
源代码,我发现在unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource)
方法中使用了XMLStreamReader
:
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");
}
}
}
So my question is, how do I override this method to add the delegate? 所以我的问题是,如何覆盖此方法以添加委托?
You can use version 2.6+ of Moxy to achieve case insensitive marshalling. 您可以使用Moxy 2.6+版本来实现不区分大小写的编组。 Heres a link to the specific enhancement that was made for the specific purpose.
这是针对特定目的进行的特定增强功能的链接 。
You will have to set UNMARSHALLING_CASE_INSENSITIVE
property to true. 您必须将
UNMARSHALLING_CASE_INSENSITIVE
属性设置为true。 Heres some sample code 继承人一些示例代码
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.