JAXB-使用通用URL进行解组
我在Java中有一种方法,可以用给定的URL对XML文件进行解组 对于像“http://…”这样的URL,一切正常,但对于像“http://…”这样的URLfile://localhost/C:/Users/.../filename.xml“我收到以下例外情况 我不知道他为什么不接受我的”file://localhost/“-URL的JAXB-使用通用URL进行解组,jaxb,unmarshalling,Jaxb,Unmarshalling,我在Java中有一种方法,可以用给定的URL对XML文件进行解组 对于像“http://…”这样的URL,一切正常,但对于像“http://…”这样的URLfile://localhost/C:/Users/.../filename.xml“我收到以下例外情况 我不知道他为什么不接受我的”file://localhost/“-URL的 用于访问文件系统的URL不正确。应该是这样的: file:///c|/path/to/file 使现代化 这个“文件:///”在mac、linux等其他系统上可
用于访问文件系统的URL不正确。应该是这样的:
file:///c|/path/to/file
使现代化
这个“文件:///”在mac、linux等其他系统上可以工作吗
您可以在任何操作系统上使用文件URL。当然,URL需要匹配那里的文件布局(即Linux中没有C驱动器)
有没有办法转换c:\。。。到c |/…/容易吗
File File=new文件(“C:/Users/../filename.xml”);
字符串url=file.toURI().toURL().toString();
您的XML文件是什么样子的?嘿,谢谢您的回答!这个“文件:///”在mac、linux等其他系统上可以工作吗?有没有办法转换c:\。。。到c |/…/容易?如何使用“file://”作为相对路径名?
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;
}
file:///c|/path/to/file