我正在使用此功能:
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&s=10'
id='MSPa2716236aaf6db55age00000019f566b957ic219h'
host='http://www4b.wolframalpha.com'
server='10'
related='http://www4b.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa2717236aaf6db55age000000535a701459c5c90a&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: 未找到协议:
我做错了什么?
请注意,我从服务器获取此responseText。因此,如果xml本身存在任何问题,请告诉我如何操作字符串,而不是建议我更改xml本身。
答案 0 :(得分:13)
问题是您传递的是XML内容本身 - 但是DocumentBuilder.parse(String)
接受 URL 来加载XML - 而不是内容本身。
你可能想要使用DocumentBuilder.parse(InputSource)
,而是从包含XML的InputSource
创建StringReader
:
Document document = builder.parse(new InputSource(new StringReader(xml)));