【问题标题】:MalformedURLException: no protocol with Xalan transformerMalformedURLException:Xalan 转换器没有协议
【发布时间】:2012-07-27 10:00:43
【问题描述】:

我在editing an XML file in a stream 上为这个问题实施解决方案时遇到了麻烦。我得到一个 MalFormedUrlException: no protocol。 XML 文件编码为 UTF-8,没有文档类型,但格式正确。我很困惑为什么会这样。

这是有问题的代码(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: [B@22732273
    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)

【问题讨论】:

    标签: java xml xerces xalan


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      异常的意思是某些东西为 XML 实体管理器提供了一个字符串,该字符串应该是一个绝对 URL……但不是。意思是 URL 没有“协议”;例如“http://example.com”或“mailto:me@example.com”中冒号前的位。

      此外,嵌套异常消息似乎在说它试图解析的假定 url 是"[B@22732273"。现在那个是一个线索,因为它是你在byte[]对象上调用toString得到的。

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

      【讨论】:

      • 测试中的b是第一个代码sn-p中的byteArray。我更改了测试,以便它将 FileInputStream 传递到 InputReader 而不是使用公共 IOUtils 并且它有效。然后在应用程序中,它在传递从 byte[] 创建的 ByteArrayInputStream 时起作用。所以我仍然没有更聪明。
      猜你喜欢
      • 2018-06-04
      • 2010-12-14
      • 1970-01-01
      • 2017-03-14
      • 2023-03-07
      • 1970-01-01
      • 2017-09-26
      • 2014-05-09
      • 2017-05-01
      相关资源
      最近更新 更多