简体   繁体   English

java.net.MalformedURLException:没有协议

[英]java.net.MalformedURLException: no protocol

I am getting Java exception like: 我得到Java异常,如:

java.net.MalformedURLException: no protocol

My program is trying to parse an XML string by using: 我的程序试图通过使用以下方法解析XML字符串:

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

The XML string contains: XML字符串包含:

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? 有关导致此错误的原因的任何建议?

The documentation could help you : http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html 该文档可以帮助您: 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. DocumentBuilder.parse(String)方法接受一个URI并尝试打开它。 If you want to directly give the content, you have to give it an InputStream or Reader , for example a StringReader . 如果要直接提供内容,则必须为其提供InputStreamReader ,例如StringReader ... Welcome to the Java standard levels of indirections ! ...欢迎来到Java标准级别的间接!

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() . 请注意,如果从文件中读取XML,则可以直接将File对象提供给DocumentBuilder.parse()

As a side note, this is a pattern you will encounter a lot in Java. 作为旁注,这是一个你将在Java中遇到很多的模式。 Usually, most API work with Streams more than with Strings. 通常,大多数API使用Streams而不是使用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 ! 使用Streams意味着可能不是所有内容都必须同时加载到内存中,这可能是一个好主意!

尝试代替db.parse(xml)

Document doc = db.parse(new InputSource(new StringReader(**xml**)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM