gpt4 book ai didi

java - JAXB : . UnmarshalException - 带有链接异常 : [javax. xml.stream.XMLStreamException

转载 作者:行者123 更新时间:2023-11-30 06:58:29 26 4
gpt4 key购买 nike

谁能帮我找出这个问题的根本原因。

XML:

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xml:base="https://*******/odata/**/">

代码:

      jaxbContext = JAXBContext.newInstance(Entry.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

XMLInputFactory xif = XMLInputFactory.newFactory();

xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(metaData));

Entry entry = (Entry) jaxbUnmarshaller.unmarshal(xsr);

类别:

@XmlRootElement(name = "entry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Entry {

异常(exception):

javax.xml.bind.UnmarshalException
- with linked exception:
[javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)

最佳答案

转换下面的字符串:

 byte[] metaBytes = metaData.getBytes();
String input = new String(metaBytes, "UTF-8");

使用SAX解析器

      SAXParserFactory parserFactory;
parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(false);
XMLReader reader = parserFactory.newSAXParser().getXMLReader();
Source er = new SAXSource(reader, new InputSource(new StringReader(input)));

关于java - JAXB : . UnmarshalException - 带有链接异常 : [javax. xml.stream.XMLStreamException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41373559/

26 4 0