与JAXB相关:如何实现JAXB兼容的变体 Package 器类?我尝试使用JAXBElement
来表示“变体”或“任意类型”。
编组正常,但在解组期间,JAXB
尝试将ElementNSImpl
分配给JAXBElement
字段。
测试代码
(复制+粘贴+运行)
import static java.lang.System.*;
import java.io.*;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.*;
import org.junit.*;
public class _JaxbElementProblem {
@XmlRootElement
@XmlType
@XmlAccessorType(XmlAccessType.NONE)
static class MyObject {
public static final QName VARIANT_NAME = new QName("QQQ");
@XmlAnyElement(lax = true)
private JAXBElement<Object> single = null;
@XmlElementWrapper(name = "elements")
@XmlAnyElement(lax = true)
final List<JAXBElement<Object>> elements =
new LinkedList<JAXBElement<Object>>();
@SuppressWarnings("unused")
private MyObject() {
}
public MyObject(Object o) {
single = new JAXBElement<Object>(VARIANT_NAME, Object.class, o);
}
public Object getSingle() {
return single.getValue();
}
public List<Object> getElements() {
List<Object> ret = new LinkedList<Object>();
for (JAXBElement<?> e : elements) {
ret.add(e.getValue());
}
return ret;
}
@Override
public String toString() {
return "MyObject (single=" + single.getValue() + "; elements: "
+ getElements() + ")";
}
}
private static final JAXBContext C;
private static final Marshaller M;
private static final Unmarshaller U;
static {
try {
C = JAXBContext.newInstance(MyObject.class);
M = C.createMarshaller();
U = C.createUnmarshaller();
M.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
} catch (JAXBException ex) {
throw new Error(ex);
}
}
private void testMarshalUnmarshal(Object root) throws Exception {
out.println("\nMARSHALLED - - - - - - - - - - - - - - - - - - - -");
StringWriter sw = new StringWriter();
M.marshal(root, sw);
out.println(sw.toString() + "\n");
out.println("\nUNMARSHALLED - - - - - - - - - - - - - - - - - - - -");
Object reunmarshalled = U.unmarshal(new StringReader(sw.toString()));
out.println(reunmarshalled + "\n");
}
@Before
public void before() {
out.println("\n= = = = = = = = = = = = = = = = = = = =");
}
@Test
public void test1() throws Exception {
MyObject root = new MyObject(Integer.valueOf(12345));
testMarshalUnmarshal(root);
}
@Test
public void test2() throws Exception {
MyObject sub = new MyObject(Integer.valueOf(12345));
MyObject root = new MyObject(sub);
testMarshalUnmarshal(root);
}
@Test
public void test3() throws Exception {
MyObject oSub = new MyObject(Integer.valueOf(12345));
List<MyObject> oSubs =
Arrays.asList(new MyObject("sub-1"), new MyObject("sub-2"),
new MyObject("sub-3"));
MyObject root = new MyObject(oSub);
for (MyObject o : oSubs) {
root.elements.add(new JAXBElement<Object>(MyObject.VARIANT_NAME,
Object.class, o));
}
testMarshalUnmarshal(root);
}
}
测试输出
(如您所见,未进行解组)
= = = = = = = = = = = = = = = = = = = =
MARSHALLED - - - - - - - - - - - - - - - - - - - -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myObject>
<QQQ xsi:type="xs:int" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">12345</QQQ>
<elements/>
</myObject>
UNMARSHALLED - - - - - - - - - - - - - - - - - - - -
= = = = = = = = = = = = = = = = = = = =
MARSHALLED - - - - - - - - - - - - - - - - - - - -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myObject>
<QQQ xsi:type="myObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<QQQ xsi:type="xs:int" xmlns:xs="http://www.w3.org/2001/XMLSchema">12345</QQQ>
<elements/>
</QQQ>
<elements/>
</myObject>
UNMARSHALLED - - - - - - - - - - - - - - - - - - - -
= = = = = = = = = = = = = = = = = = = =
MARSHALLED - - - - - - - - - - - - - - - - - - - -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myObject>
<QQQ xsi:type="myObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<QQQ xsi:type="xs:int" xmlns:xs="http://www.w3.org/2001/XMLSchema">12345</QQQ>
<elements/>
</QQQ>
<elements>
<QQQ xsi:type="myObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<QQQ xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">sub-1</QQQ>
<elements/>
</QQQ>
<QQQ xsi:type="myObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<QQQ xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">sub-2</QQQ>
<elements/>
</QQQ>
<QQQ xsi:type="myObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<QQQ xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">sub-3</QQQ>
<elements/>
</QQQ>
</elements>
</myObject>
UNMARSHALLED - - - - - - - - - - - - - - - - - - - -
解组异常
java.lang.IllegalArgumentException: Can not set javax.xml.bind.JAXBElement field my._JaxbElementProblem$MyObject.single to com.sun.org.apache.xerces.internal.dom.ElementNSImpl
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
at java.lang.reflect.Field.set(Field.java:657)
at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection.set(Accessor.java:234)
at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor.receive(Accessor.java:160)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:507)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:145)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2938)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
at my._JaxbElementProblem.testMarshalUnmarshal(_JaxbElementProblem.java:84)
at my._JaxbElementProblem.test3(_JaxbElementProblem.java:119)
1条答案
按热度按时间lkaoscv71#
运行示例:
(Hint:JAXBHelper是从utils-apl派生的,但只是解组和编组例程的简短形式)
输出: