检索 XML 运行 5 小时
我从服务器收到 360K 数据,并将其转换为手机上的 XML 文件。最终文件约为 1.3 Mb。后来,我尝试在 GT-S5660 上读取此文件,此过程运行了 5(!)小时。尽管它产生了正确的结果,但我想知道是否有办法优化这个过程。我有一个 iPhone 版本,整个过程不到 5 秒即可完成。 这是正常行为吗?如果是这样,SQLite 会是替代方案吗? 这是我的代码:
FileInputStream fileos;
try {
fileos = openFileInput("TarifsCache" + Constants.VERSION + ".txt");
} catch (FileNotFoundException e) {
return;
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
return;
}
Document doc;
try {
doc = db.parse(new InputSource(fileos));
} catch (SAXException e) {
return;
} catch (IOException e) {
return;
}
doc.getDocumentElement().normalize();
for (int i = 0;; i++) {
NodeList nodeList = doc.getElementsByTagName("Cache" + i);
Node node = nodeList.item(0);
if (node == null)
break;
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("area_ref");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
tarifread.area_ref = Integer.parseInt(((Node) nameList.item(0))
.getNodeValue());
后面是大量其他元素检索。
I receive 360K of data from a server which I transform into a XML file on my phone. The final file is about 1.3 Mb. Later, I try to read this file and on my GT-S5660 and this process runs for 5(!) hours. Although it produces a correct result I wonder if there is a way to optimize this process. I have an iphone version of it, and there the process completes in less than 5 seconds.
Is this a normal behaviour? If so, would SQLIte be an alternative?
Here's my code:
FileInputStream fileos;
try {
fileos = openFileInput("TarifsCache" + Constants.VERSION + ".txt");
} catch (FileNotFoundException e) {
return;
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
return;
}
Document doc;
try {
doc = db.parse(new InputSource(fileos));
} catch (SAXException e) {
return;
} catch (IOException e) {
return;
}
doc.getDocumentElement().normalize();
for (int i = 0;; i++) {
NodeList nodeList = doc.getElementsByTagName("Cache" + i);
Node node = nodeList.item(0);
if (node == null)
break;
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("area_ref");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
tarifread.area_ref = Integer.parseInt(((Node) nameList.item(0))
.getNodeValue());
followed by plenty of other element retrievals.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用高级解析器解析器,例如 SAX 和 dnt 使用 STAX,因为它在 android 中不支持。
Try to use advance parser parser like SAX and dnt use STAX as its not support in android .