2012-07-27 29 views
1

我在editing an XML file in a stream上执行此问题的解决方案时遇到问题。我得到一个MalFormedUrlException:没有协议。 XML文件编码为UTF-8,没有文档类型,但格式正确。我很难理解为什么会发生这种情况。MalformedURLException:无Xalan变压器的协议

这里是有问题的代码(byteArray有XML,UpdatingXmlReader是我的课):

XMLReader reader = 
     new UpdatingXmlReader(SAXParserFactory.newInstance().newSAXParser()); 
    Transformer xform = TransformerFactory.newInstance().newTransformer(); 

    InputSource inputSource = 
     new InputSource(new ByteArrayInputStream(byteArray)); 
    StreamResult streamResult = 
     new StreamResult(response.getOutputStream()); 

    SAXSource saxSource = new SAXSource(reader, inputSource);      

    xform.transform(saxSource, streamResult); 

它是如何被称为在我的测试:

File file = new File("c:/test.xml"); 
    InputStream input = new FileInputStream(file); 
    byte[] b = IOUtils.toByteArray(input); 
    // in production the byte array will come from the database 
    service.method(b, httpServletResponse ,httpServletRequest) 

这里的堆栈跟踪:

javax.xml.transform.TransformerException: 
    java.net.MalformedURLException: no protocol: 
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(Unknown Source) 
Caused by: java.net.MalformedURLException: no protocol: [[email protected] 
    at java.net.URL.<init>(URL.java:579) 
    at java.net.URL.<init>(URL.java:476) 
    at java.net.URL.<init>(URL.java:425) 
    at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source) 
    at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source) 
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) 
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) 
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) 
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) 
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) 
    at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source) 

回答

0

尝试将“file://”追加到文件路径的开头。

0

这个例外的意思是,有些东西已经为XML实体管理器提供了一个应该是绝对URL的字符串......但事实并非如此。这是说该网址没有“协议”;例如“http://example.com”或“mailto:[email protected]”冒号之前的位。

此外,嵌套的异常消息似乎是说,它试图解析的应该的网址是"[[email protected]"。现在线索,因为这是你所得到的,如果你在byte[]对象上调用toString

所以我暂时的诊断是,你没有向我们展示的代码传递一个字节数组,它实际上应该传递一个将解析为URL字符串的对象。

+0

测试中的b是第一个代码片段中的byteArray。我改变了测试,以便它将FileInputStream传递到InputReader,而不是使用公共IOUtils并且它工作。然后在应用程序中传递从byte []创建的ByteArrayInputStream。所以我依然不明智。 – blank 2012-07-27 15:50:09