java.net.MalformedURLException:未找到协议。原因?

java.net.MalformedURLException: Protocol not found. Reason?

我正在使用这个功能:

public void testing(String xml) throws ParserConfigurationException, SAXException, IOException{
        Log.d("TAG"," root.getNodeName()");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document document = builder.parse(xml);

        //document.getDocumentElement().normalize();
        //Element root = document.getDocumentElement();
        //Log.d("TAG", root.getNodeName());
        Log.d("TAG"," root.getNodeName()");

    }

我是这样调用这个函数的:

testing(responseText) 

回复文本是这样的:

<?xml version='1.0' encoding='UTF-8'?>
<queryresult success='true'
    error='false'
    numpods='2'
    datatypes=''
    timedout=''
    timedoutpods=''
    timing='0.751'
    parsetiming='0.216'
    parsetimedout='false'
    recalculate='http://www4b.wolframalpha.com/api/v2/recalc.jsp?id=MSPa2715236aaf6db55age00000025hbhc18c61h80c4&amp;s=10'
    id='MSPa2716236aaf6db55age00000019f566b957ic219h'
    host='http://www4b.wolframalpha.com'
    server='10'
    related='http://www4b.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa2717236aaf6db55age000000535a701459c5c90a&amp;s=10'
    version='2.6'>
 <pod title='Input interpretation'
     scanner='Identity'
     id='Input'
     position='100'
     error='false'
     numsubpods='1'>
  <subpod title=''>
   <plaintext>Tell me a joke.</plaintext>
  </subpod>
 </pod>
 <pod title='Result'
     scanner='Data'
     id='Result'
     position='200'

但我收到错误消息:

04-06 22:19:14.348: D/TAG(30413): java.net.MalformedURLException: Protocol not found:

我做错了什么?

请注意,我正在从服务器获取此 responseText。因此,如果 xml 本身有任何问题,请告诉我如何操作字符串,而不是建议我更改 xml 本身。

问题是您传递的是 XML 内容本身 - 但 DocumentBuilder.parse(String) 接受 URL 来加载 XML 来自 - 而不是内容本身。

您可能想改用 DocumentBuilder.parse(InputSource),从 StringReader 包装 XML:

创建了一个 InputSource
Document document = builder.parse(new InputSource(new StringReader(xml)));