215

I am getting Java exception like:

java.net.MalformedURLException: no protocol

My program is trying to parse an XML string by using:

Document dom;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse(xml);

The XML string contains:

String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
    "   <s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
    "       <s:Header>"+
    "           <ActivityId CorrelationId=\"15424263-3c01-4709-bec3-740d1ab15a38\" xmlns=\"http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics\">50d69ff9-8cf3-4c20-afe5-63a9047348ad</ActivityId>"+
    "           <clalLog_CorrelationId xmlns=\"http://clalbit.co.il/clallog\">eb791540-ad6d-48a3-914d-d74f57d88179</clalLog_CorrelationId>"+
    "       </s:Header>"+
    "       <s:Body>"+
    "           <ValidatePwdAndIPResponse xmlns=\"http://tempuri.org/\">"+
    "           <ValidatePwdAndIPResult xmlns:a=\"http://schemas.datacontract.org/2004/07/ClalBit.ClalnetMediator.Contracts\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"+
    "           <a:ErrorMessage>Valid User</a:ErrorMessage>"+
    "           <a:FullErrorMessage i:nil=\"true\" />"+
    "           <a:IsSuccess>true</a:IsSuccess>"+
    "           <a:SecurityToken>999993_310661843</a:SecurityToken>"+
    "           </ValidatePwdAndIPResult>"+
    "           </ValidatePwdAndIPResponse>"+
    "       </s:Body>\n"+
    "   </s:Envelope>\n";

Any suggestions about what is causing this error?

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • Where exactly are you getting that error message? While parsing the XML, or while trying to send it over the network? It would be really strange if you get this while parsing. – Jesper Nov 10 '09 at 08:59
  • duplicated? https://stackoverflow.com/questions/964311/exception-no-protocol-while-reading-xml/964377 – Fernando Gonzalez Sanchez Jun 12 '19 at 09:34

2 Answers2

439

The documentation could help you : http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html

The method DocumentBuilder.parse(String) takes a URI and tries to open it. If you want to directly give the content, you have to give it an InputStream or Reader, for example a StringReader. ... Welcome to the Java standard levels of indirections !

Basically :

DocumentBuilder db = ...;
String xml = ...;
db.parse(new InputSource(new StringReader(xml)));

Note that if you read your XML from a file, you can directly give the File object to DocumentBuilder.parse() .

As a side note, this is a pattern you will encounter a lot in Java. Usually, most API work with Streams more than with Strings. Using Streams means that potentially not all the content has to be loaded in memory at the same time, which can be a great idea !

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
Guillaume
  • 18,494
  • 8
  • 53
  • 74
  • 4
    @Guillaume, doing that I get document = null. Why does this happen? – HenioJR Nov 10 '14 at 19:23
  • 1
    No idea without looking at the code. Maybe your input is invalid and you swallow exception somehow ... – Guillaume Nov 13 '14 at 16:24
  • This line of code takes long time to execute. Any way to improve performance? – Raj Shah Oct 29 '15 at 17:45
  • 1
    +1 for me assuming (foolishly, of course) that the string parameter was supposed to the xml to parse.... Of course, it makes _perfect_ sense to expect a URI instead.... – Nicholas Terry Apr 08 '16 at 06:00
  • @RajShah You should only run this logic at startup, so a larger time hit shouldnt matter. You shouldn't have to change the configuration very often, either.... – Nicholas Terry Apr 08 '16 at 06:01
  • The document printed out as `[#document: null]`, but `document.getDocumentElement()` returned the expected XML. – user2521119 Oct 04 '21 at 11:17
29

Try instead of db.parse(xml):

Document doc = db.parse(new InputSource(new StringReader(**xml**)));
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Shyam_coder
  • 869
  • 10
  • 5