1

I'm having trouble implementing a solution for this question on editing an XML file in a stream. I get a MalFormedUrlException: no protocol. The XML file is encoded as UTF-8, has no doctype but is well-formed. I'm stumped as to why this is happening.

Here's the offending code (byteArray has the xml, UpdatingXmlReader is my class):

    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);

How it's called in my test:

    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)

Here's the stacktrace:

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)
Community
  • 1
  • 1
blank
  • 17,852
  • 20
  • 105
  • 159

2 Answers2

0

Try append "file://" to the beginning of your file path.

zizoujab
  • 7,603
  • 8
  • 41
  • 72
0

What the exception is saying is that something has supplied the XML entity manager with a string that is supposed to be an absolute URL ... but isn't. It is saying that the URL doesn't have a "protocol"; e.g. the bit before the colon in "http://example.com" or "mailto:me@example.com".

Furthermore, the nested exception message seems to be saying that the supposed url it is attempting to parse is "[B@22732273". Now that is a big clue, because it is what you get if you call toString on a byte[] object.

So my tentative diagnosis is that some code that you haven't shown us is passing a byte array where it should actually be passing an object that will unparse as a URL string.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • the b in the test is the byteArray in the first code snippet. I changed the test so that it passes the FileInputStream into the InputReader instead of using the commons IOUtils and it worked. And in the application it then worked when passing the ByteArrayInputStream created from the byte[]. So I'm still none the wiser. – blank Jul 27 '12 at 15:50