正如標題所述,我想忽略文檔中元素名稱的外框。如何忽略JaxB中元素名稱的外框
static class XY433 {
@XmlAttribute(name = "C200")
String c200;
@XmlAttribute(name = "C215")
String c215;
@XmlAttribute(name="F001")
String f001;
@XmlAttribute(name="f001")
String lcf001; // I want to avoid this duplication
}
我試圖用張貼布萊斯Doughan代碼:
private static class ToLowerCaseNamesStreamReaderDelegate extends StreamReaderDelegate {
public ToLowerCaseNamesStreamReaderDelegate(XMLStreamReader xsr) {
super(xsr);
}
@Override
public String getAttributeLocalName(int index) {
return super.getAttributeLocalName(index).toLowerCase();
}
@Override
public String getLocalName() {
return super.getLocalName().toLowerCase();
}
}
@XmlRootElement(name="doc")
static class Doc {
@XmlElement(name="element")
List<Element> elements;
}
static class Element {
@XmlAttribute(name = "abc")
String abc;
}
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("LowerCaseElementNamesFilterTest.xml"));
Unmarshaller u = JAXBContext.newInstance(Doc.class).createUnmarshaller();
//Do unmarshalling
Doc doc = (Doc) u.unmarshal(new ToLowerCaseNamesStreamReaderDelegate(xsr));
System.out.println(doc.elements.get(0).abc);
System.out.println(doc.elements.get(1).abc);
System.out.println(doc.elements.get(2).abc);
}
這實際上沒有工作。
null
2
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.RangeCheck(ArrayList.java:546)
at java.util.ArrayList.get(ArrayList.java:321)
at com.hre.commons.tec.xml.LowerCaseElementNamesFilter.main(LowerCaseElementNamesFilter.java:58)
對於這個XML:
<doc>
<Element ABC="1"></Element>
<element Abc="1"></element>
<element abc="2"></element>
</doc>
看看http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlAccessType.html#NONE和這個queistion是非常類似http://stackoverflow.com/questions/659872/how-do-i-prevent-jaxb-from-binding-superclass-methods-of-xmlrootelement-when :) – 2010-12-07 10:13:18
我的問題與這個超類問題有什麼關係?我怎樣才能使用AccessType?我的問題是關於f001和lcf001。我只想要一個字段綁定到「F001」和「f001」。 – tkr 2010-12-07 12:33:01