JAXB -Unmarshalling与一般URL

时间:2015-03-29 01:11:27

标签: jaxb unmarshalling

我在Java中有一个用于解组具有给定URL的XML文件的方法。

对于像“http:// ...”这样的URL,一切正常,但对于像“file:// localhost / C:/Users/.../filename.xml”这样的URL,我收到以下异常。

我不知道他为什么不接受我的“file:// localhost /”-URL's。

javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204)
    at preferee.data.access.IO_transfer.jaxb.XMLconverter.getItemFromStream(XMLconverter.java:40)
    at preferee.data.access.IO_transfer.jaxb.XMLconverter.getItemFromURL(XMLconverter.java:57)
    at preferee.data.access.testServer.LocalTestServer.<init>(LocalTestServer.java:42)
    at preferee.data.access.testServer.TestProvider.<init>(TestProvider.java:16)
    at preferee.data.access.Providers.createTestProvider(Providers.java:29)
    at preferee.tests.FakeServerTests.MovieDao_TEST.run(MovieDao_TEST.java:22)
    at preferee.tests.FakeServerTests.MovieDao_TEST.main(MovieDao_TEST.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1436)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:999)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)

顺便说一下,这是我的方法实现:

Class classObject  = ... ;

public T getItemFromURL(String url) throws DataAccessException {
    JAXBContext jc = null;
    T item = null;
    try (InputStream XML_Stream = new URL(url).openStream();)
    {
        jc = JAXBContext.newInstance(classObject);
        item = (T) jc.createUnmarshaller().unmarshal(XML_Stream);
    } catch (IOException e) {
        throw new DataAccessException("( originele error: " + e.getClass() +" ) " + e.getMessage() + ": Kon Bestand niet ophalen of lezen." );
    } catch (JAXBException e) {
        throw new DataAccessException(e.getMessage());
    }
    return item;
}

1 个答案:

答案 0 :(得分:0)

您点击文件系统的网址不正确。应该是这样的:

file:///c|/path/to/file

更新

  

这是&#34; file:///&#34;在其他系统上工作,比如mac,linux?

您可以在任何操作系统上使用文件URL。当然,URL需要匹配那里的文件布局(即Linux中没有C驱动器)。

  

有没有办法将c:\ ...转换成c | / ... /轻松?

File file = new File("C:/Users/.../filename.xml");
String url = file.toURI().toURL().toString();