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

时间:2016-12-29 05:22:48

标签: java xml exception jaxb

任何人都可以帮我找出这个问题的根本原因。

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)

1 个答案:

答案 0 :(得分:1)

转换下面的字符串:

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

使用SAX Parser

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