2015-04-06 79 views
2

我使用这个功能:java.net.MalformedURLException:未找到协议。原因?

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本身。

回答

11

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

你可能想使用DocumentBuilder.parse(InputSource)相反,在创建了从0​​包裹XML的InputSource

Document document = builder.parse(new InputSource(new StringReader(xml))); 
+1

从自己的正确答案。谢谢,它的作品! 只要我允许我接受你的答案:P –

相关问题