java.net.MalformedURLException: 无协议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1706493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:45:04  来源:igfitidea点击:

java.net.MalformedURLException: no protocol

javaxmlexception

提问by

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?

有关导致此错误的原因的任何建议?

回答by Guillaume

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. If you want to directly give the content, you have to give it an InputStreamor Reader, for example a StringReader. ... Welcome to the Java standard levels of indirections !

该方法DocumentBuilder.parse(String)接受一个 URI 并尝试打开它。如果你想直接给内容,你必须给它一个InputStreamor Reader,例如 a StringReader。... 欢迎来到 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 Fileobject to DocumentBuilder.parse().

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

附带说明一下,这是您在 Java 中经常会遇到的一种模式。通常,大多数 API 使用 Streams 多于使用 Strings。使用 Streams 意味着可能并非所有内容都必须同时加载到内存中,这可能是一个好主意!

回答by Shyam_coder

Try instead of db.parse(xml):

尝试代替db.parse(xml)

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