[英]java.net.MalformedURLException: no protocol
我得到Java異常,如:
java.net.MalformedURLException: no protocol
我的程序試圖通過使用以下方法解析XML字符串:
Document dom;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse(xml);
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";
有關導致此錯誤的原因的任何建議?
該文檔可以幫助您: http : //java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html
DocumentBuilder.parse(String)
方法接受一個URI並嘗試打開它。 如果要直接提供內容,則必須為其提供InputStream
或Reader
,例如StringReader
。 ...歡迎來到Java標准級別的間接!
基本上:
DocumentBuilder db = ...;
String xml = ...;
db.parse(new InputSource(new StringReader(xml)));
請注意,如果從文件中讀取XML,則可以直接將File
對象提供給DocumentBuilder.parse()
。
作為旁注,這是一個你將在Java中遇到很多的模式。 通常,大多數API使用Streams而不是使用Strings。 使用Streams意味着可能不是所有內容都必須同時加載到內存中,這可能是一個好主意!
嘗試代替db.parse(xml)
:
Document doc = db.parse(new InputSource(new StringReader(**xml**)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.