android4.0和android2.3一下XML解析的区别

android4.0不支持一下这种document解析:

InputStream inStream = new ByteArrayInputStream(strData.getBytes());
DocumentBuilderFactory docBFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
Document doc = null;
try
{
docBuilder = docBFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
}
try
{
doc = docBuilder.parse(inStream);
} catch (SAXException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
} catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
}
CarTrackInfoACK obj = new CarTrackInfoACK();
Element root = null;
try
{
Element docEle = doc.getDocumentElement();
NodeList nList = docEle.getElementsByTagName("root");
root = (Element) nList.item(0);
} catch (Exception e)
{
e.printStackTrace();
}
try
{
Element resultCode = (Element) root.getElementsByTagName("resultCode").item(0);
int iret = Integer.parseInt(resultCode.getFirstChild().getNodeValue());
obj.mnRet = iret == 0 ? Protocol_base.RET_SUCCESS : iret;
} catch (Exception e)
{
e.printStackTrace();
}


try
{
Element msg = (Element) root.getElementsByTagName("msg").item(0);
obj.mstrMsg = msg.getFirstChild().getNodeValue();
} catch (Exception e)
{
e.printStackTrace();
}


Element trackInfo = null;
try
{
Element data = (Element) root.getElementsByTagName("data").item(0);
trackInfo = (Element) data.getElementsByTagName("trackInfo").item(0);
if (trackInfo == null)
return obj;
} catch (Exception e)
{
e.printStackTrace();
}


try
{
obj.mTrack.time = trackInfo.getElementsByTagName("time").item(0).getFirstChild().getNodeValue();
} catch (Exception e)
{
e.printStackTrace();
}


try
{
obj.mTrack.speed = trackInfo.getElementsByTagName("speed").item(0).getFirstChild().getNodeValue();
} catch (Exception e)
{
e.printStackTrace();
}


try
{
obj.mTrack.userId = trackInfo.getElementsByTagName("userId").item(0).getFirstChild().getNodeValue();
} catch (Exception e)
{
e.printStackTrace();
}


try
{
obj.mTrack.elevation = trackInfo.getElementsByTagName("elevation").item(0).getFirstChild().getNodeValue();
} catch (Exception e)
{
e.printStackTrace();
}


try
{
obj.mTrack.lng = trackInfo.getElementsByTagName("lng").item(0).getFirstChild().getNodeValue();
} catch (Exception e)
{
e.printStackTrace();
}


try
{
obj.mTrack.lat = trackInfo.getElementsByTagName("lat").item(0).getFirstChild().getNodeValue();
} catch (Exception e)
{
e.printStackTrace();
}
android4.0建议用一下方式解析:

InputStream inputStream = new ByteArrayInputStream(strData.getBytes());
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser;
XMLReader reader = null;
try
{
parser = factory.newSAXParser();
reader = parser.getXMLReader();
} catch (ParserConfigurationException e)
{
e.printStackTrace();
} catch (SAXException e)
{
e.printStackTrace();
}


final CarTrackInfoACK obj = new CarTrackInfoACK();
RootElement rootElement = new RootElement("root");


android.sax.Element resultCodeElement = rootElement.getChild("resultCode");
// 读到文本的末尾时触发,这里的vip即为文本的内容部分
resultCodeElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mnRet = Integer.parseInt(vip) == 0 ? Protocol_base.RET_SUCCESS : Integer.parseInt(vip);
}


});


android.sax.Element msgElement = rootElement.getChild("msg");
// 读到文本的末尾时触发,这里的vip即为文本的内容部分
msgElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mstrMsg = vip;
}


});


android.sax.Element dataElement = rootElement.getChild("data");
android.sax.Element trackInfoElement = dataElement.getChild("trackInfo");
android.sax.Element userIdElement = trackInfoElement.getChild("userId");
userIdElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mTrack.userId = vip;
}


});
android.sax.Element timeElement = trackInfoElement.getChild("time");
timeElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mTrack.time = vip;
}


});
android.sax.Element latElement = trackInfoElement.getChild("lat");
latElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mTrack.lat = vip;
}


});
android.sax.Element lngElement = trackInfoElement.getChild("lng");
lngElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mTrack.lng = vip;
}


});
android.sax.Element speedElement = trackInfoElement.getChild("speed");
speedElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mTrack.speed = vip;
}


});
android.sax.Element elevationElement = trackInfoElement.getChild("elevation");
elevationElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mTrack.elevation = vip;
}


});
android.sax.Element elatElement = trackInfoElement.getChild("elat");
elatElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mTrack.elat = vip;
}


});
android.sax.Element elngElement = trackInfoElement.getChild("elng");
elngElement.setEndTextElementListener(new EndTextElementListener()
{
@Override
public void end(String vip)
{
obj.mTrack.elng = vip;
}


});
/*
* // 读到元素开始位置时触发,如读到<person>时 personElement.setStartElementListener(new
* StartElementListener() {

* @Override public void start(Attributes attributes) { obj = new
* CarTrackInfoACK(); } });

* //读到元素结束位置时触发,如读到</person>时 personElement.setEndElementListener(new
* EndElementListener() {

* @Override public void end() { PersonList.add(person); } });
*/


if (reader != null)
{
reader.setContentHandler(rootElement.getContentHandler());


try
{
reader.parse(new InputSource(inputStream));
} catch (IOException e)
{
e.printStackTrace();
} catch (SAXException e)
{
e.printStackTrace();
}
}

你可能感兴趣的:(android4.0和android2.3一下XML解析的区别)