问题描述
java.net.MalformedURLException:没有协议
我的程序正在尝试通过使用以下方式解析XML字符串:
文件dom
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse(xml);
XML字符串包含:
String xml =<?xml version = \1.0\encoding = \utf-8\?>+
< s:信封xmlns:s = \http://schemas.xmlsoap.org/soap/envelope/\>+
< s:标题>+
< 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.Claln etMediator.Contracts\xmlns:i = \http://www.w3.org/2001/XMLSchema-instance\>+
< a:ErrorMessage>有效用户< / a:ErrorMessage>+
< a:FullErrorMessage i:nil = \true\/>+
< a:IsSuccess> true< / a:IsSuccess> +
< a:SecurityToken> 999993_310661843< / a:SecurityToken>+
< / ValidatePwdAndIPResult>+
< / ValidatePwdAndIPResponse>+
< / s:Body> \\\
+
< / s:Envelope> \\\
;
有关导致此错误的建议?
文档可以帮助您:
方法 DocumentBuilder.parse(String)
需要一个URI并尝试打开它。如果你想直接给出内容,你必须给它一个 InputStream
,例如一个 ByteArrayInputStream
。 ...欢迎来到Java标准级别的指导!
基本上:
DocumentBuilder db = ...;
String xml = ...;
db.parse(new InputSource(new ByteArrayInputStream(xml.getBytes(utf-8))));
请注意,如果从文件中读取XML,可以直接给出文件
对象到 DocumentBuilder.parse()
。
作为附注,这是您将在Java中遇到的一个模式。通常,大多数API使用Streams比使用Strings更多。使用流意味着可能并不是所有的内容都必须同时加载到内存中,这可能是个好主意!
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?
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
, for example a ByteArrayInputStream
. ... Welcome to the Java standard levels of indirections !
Basically :
DocumentBuilder db = ...;
String xml = ...;
db.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
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 !
这篇关于java.net.MalformedURLException:没有协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!