gpt4 book ai didi

java - 以编程方式添加或设置 JAXB XmlRootElement

转载 作者:太空宇宙 更新时间:2023-11-04 06:36:16 26 4
gpt4 key购买 nike

我想避免向每个用于解码 xml 的 POJO 添加注释 @XMLRootElement。有没有办法以编程方式设置或添加 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)

编辑:

布莱斯的回答确实有效。

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();
}

并且它抛出一个异常cannot org.apache.xerces.dom.ElementNSImpl不能被转换为..我做错了什么?

最佳答案

您只需使用采用 Class 参数的 unmarshal 方法之一。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com