我想避免将注释@XMLRootElement添加到将用于解组xml的每个POJO中。有没有一种方法可以设置XmlRootElement或以编程方式将其添加到类?

 //@XmlRootElement here
    public class Person{
    private String name;
    private String address;
    //other fields
    //getters and setters here


如果不可能,如何在POJO中没有XmlRootElement的情况下解组xml?

public static <T> T unmarshal(Class clazz, String xml) {
    try {
        JAXBContext ctx = JAXBContext.newInstance(clazz);
        Unmarshaller u = ctx.createUnmarshaller();
        return (T) u.unmarshal(new StringReader(xml));
    } catch (JAXBException e) {
        throw new RuntimeException("Error interpreting XML response", e);
    }
}


没有@XmlRootElement引发的异常:

Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"report"). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:556)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:199)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:194)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:71)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:962)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:399)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:380)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:195)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)


编辑:

Blaise的答案实际上是有效的。

StreamSource source = new StreamSource(new StringReqder(xml));
return (T) u.unmarshal(source, clazz).getValue();


但是当我试图用它返回一个列表时

//this works when I specify @XmlRootElement
private static <T> List<T> unmarshallCollection(Class<T> clazz, Source source)
            throws JAXBException {
        JAXBContext ctx = JAXBContext.newInstance(WrapperCollection.class, clazz);
        Unmarshaller u = ctx.createUnmarshaller();
        WrapperCollection<T> collection = u.unmarshal(source, WrapperCollection.class).getValue();
        return collection.getItems();
    }


并且它引发一个异常,无法将org.apache.xerces.dom.ElementNSImpl强制转换为..
我做错了什么?

最佳答案

您只需要使用带有unmarshal参数的Class方法之一。

StreamSource source = new StreamSource(new StringReqder(xml));
return (T) u.unmarshal(source, clazz).getValue();


注意


unmarshal方法的结果将是JAXBElement的实例,该实例包含根元素信息,您可以通过在其上调用getValue来获取未编组对象。
为了进行编组,您可以通过将对象包装在JAXBElement实例中,然后进行编组来提供根信息。

关于java - 以编程方式添加或设置JAXB XmlRootElement,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25356118/

10-09 07:26