Java Android中XML流的解析
我不知道为什么,但是Log.I(TAG,“find:”+find);为XML中的“found”元素返回null,该元素是我在流中得到的。我已经调试过了,似乎我的XML根元素已被读取,但该根元素的元素未被读取。urlString也是正确的。解析文档时没有任何错误和异常。但在这方面Java Android中XML流的解析,java,android,xml,inputstream,Java,Android,Xml,Inputstream,我不知道为什么,但是Log.I(TAG,“find:”+find);为XML中的“found”元素返回null,该元素是我在流中得到的。我已经调试过了,似乎我的XML根元素已被读取,但该根元素的元素未被读取。urlString也是正确的。解析文档时没有任何错误和异常。但在这方面 NodeList nl = docEle.getElementsByTagName("found"); found = nl.item(0).getNodeValue(); 找到的值为空。这是方法的完整
NodeList nl = docEle.getElementsByTagName("found");
found = nl.item(0).getNodeValue();
找到的值为空。这是方法的完整代码:
protected Void doInBackground(String... urls) {
String urlString = urls[0];
URL documentUrl = null;
InputStream stream = null;
URLConnection conn = null;
DocumentBuilder builder = null;
Document document = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
documentUrl = new URL(urlString);
conn = documentUrl.openConnection();
stream = conn.getInputStream();
document = builder.parse(stream);
}
catch (IOException e) {
Error = e.getMessage();
Log.i(TAG, "Exception!", e);
}
catch (SAXException e) {
Error = e.getMessage();
Log.i(TAG, "Exception!", e);
}
catch (ParserConfigurationException e) {
Error = e.getMessage();
Log.i(TAG, "Exception!", e);
}
finally
{
if (stream != null)
{
try {
stream.close();
} catch (IOException e) {
Error = e.getMessage();
Log.i(TAG, "Exception!", e);
}
}
}
/*NodeList nodes = document.getElementsByTagName("found");
for (int i = 0; i < nodes.getLength(); i++) {
found = nodes.item(i).getNodeValue();
//System.out.println(found);
Log.i(TAG, "found: "+found);
}*/
//get the root element
Element docEle = document.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("found");
found = nl.item(0).getNodeValue();
Log.i(TAG, "found: "+found);
return null;
}
protectedvoiddoinbackground(字符串…URL){
字符串urlString=url[0];
URL documentUrl=null;
InputStream=null;
URLConnection conn=null;
DocumentBuilder=null;
单据=空;
试一试{
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
builder=factory.newDocumentBuilder();
documentUrl=新URL(urlString);
conn=documentUrl.openConnection();
stream=conn.getInputStream();
document=builder.parse(流);
}
捕获(IOE异常){
错误=e.getMessage();
Log.i(标记“Exception!”,e);
}
捕获(SAXE异常){
错误=e.getMessage();
Log.i(标记“Exception!”,e);
}
捕获(ParserConfiguration异常e){
错误=e.getMessage();
Log.i(标记“Exception!”,e);
}
最后
{
if(流!=null)
{
试一试{
stream.close();
}捕获(IOE异常){
错误=e.getMessage();
Log.i(标记“Exception!”,e);
}
}
}
/*NodeList nodes=document.getElementsByTagName(“找到”);
对于(int i=0;i
这是我的XML URL字符串:
找到的值必须等于20 使用:
found = nl.item(0).getFirstChild().getNodeValue();