我得到的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";对于导致这个错误的原因有什么建议吗?
发布于 2009-11-10 17:05:27
文档可以帮助您: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意味着可能不是所有内容都必须同时加载到内存中,这可能是一个很好的想法!
发布于 2016-06-27 20:39:08
尝试而不是db.parse(xml)
Document doc = db.parse(new InputSource(new StringReader(**xml**)));https://stackoverflow.com/questions/1706493
复制相似问题