【问题标题】:JAXB : .UnmarshalException - with linked exception: [javax.xml.stream.XMLStreamExceptionJAXB:.UnmarshalException - 带有链接异常:[javax.xml.stream.XMLStreamException
【发布时间】:2017-05-13 09:59:19
【问题描述】:

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

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 {

例外:

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)

【问题讨论】:

  • 确保您的 XML 没有 a BOM at the start。您的 XML 也缺少结束根标记。
  • 检查了 xml 文件,没有垃圾字符。 @近似蓝

标签: java xml exception jaxb


【解决方案1】:

在下面转换你的字符串:

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

【讨论】:

    猜你喜欢
    • 2014-02-24
    • 2011-07-03
    • 2014-09-29
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 2012-11-29
    相关资源
    最近更新 更多