java.net.MalformedURLException:使用 JAXB 时没有带有 javax.xml.stream.XMLStreamException 的协议

Bha*_*jee 2 xml jaxb malformedurlexception xml-parsing xmlstreamreader

我正在使用 JAXB 来解组 XML 文档。解析 XML 时,它会抛出用 XMLStreamException 包装的 MalformedURLException。我的理解是,在创建 XMLStreamReader 对象本身时,它会抛出异常。请问有什么建议吗?

我正在使用的代码片段:

    XMLInputFactory xif = XMLInputFactory.newFactory();      
    XMLResolver resolver = new XMLResolver(); //to capture systemID, base URI etc.
    xif.setXMLResolver(resolver);

    //Throws MalformedURLException  while processing the below line
    XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(fileToProcess));

    JAXBContext jaxbContext = JAXBContext.newInstance(MyPackage.MyClassName.class);
Run Code Online (Sandbox Code Playgroud)

这是异常跟踪:

     class javax.xml.stream.XMLStreamException
     javax.xml.stream.XMLStreamException: java.net.MalformedURLException: no protocol:       [XML_FILEPATH/XML_FILE_NAME]
Run Code Online (Sandbox Code Playgroud)

fileToProcess是包含绝对路径的字符串,例如 /home/project/input/myproject.xml

运行时 JDK 是 1.7。我缺少任何签名/协议吗?

谢谢,巴斯卡

Nar*_*ren 5

尝试这个:

XMLStreamReader streamReader = factory.createXMLStreamReader(
new FileReader("data\\test.xml"));
Run Code Online (Sandbox Code Playgroud)