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

har*_*ash 2 java xml url dom

我正在使用这个功能:

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()");

    }
Run Code Online (Sandbox Code Playgroud)

我这样称呼这个函数:

testing(responseText) 
Run Code Online (Sandbox Code Playgroud)

响应文本是这样的:

<?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'
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

04-06 22:19:14.348:D/TAG(30413):java.net.MalformedURLException:未找到协议:

我究竟做错了什么 ?

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

Jon*_*eet 13

问题是你传递XML内容本身 - 但DocumentBuilder.parse(String)接受一个URL来加载XML - 而不是内容本身.

你可能想要使用DocumentBuilder.parse(InputSource),创建InputSource一个StringReader包装XML:

Document document = builder.parse(new InputSource(new StringReader(xml)));
Run Code Online (Sandbox Code Playgroud)