简体   繁体   English

Android中的XPath XML数据解析非常慢

[英]XPath XML data parsing in android very slow

The xpath.evaluate parsing in android takes about 8-9 seconds for each. android中的xpath.evaluate解析每个过程大约需要8-9秒。 I use about 8 different xpath evaluate methods and my program takes about 1 min 35 seconds to load. 我使用了大约8种不同的xpath评估方法,并且我的程序需要大约1分35秒来加载。

I'm using an AsyncTask for this purpose and I don't want to implement other parsers which are not included in the standard JDK. 我为此使用AsyncTask,并且我不想实现标准JDK中未包含的其他解析器。

One of the many laggy xpath evaluate code blocks : 许多落后的xpath评估代码块之一:

for (int i=1;i<=36;i++){
                      InputSource source16 = new InputSource(new StringReader(hourly));

                      try{
                       humidity = xpath.evaluate("/response/hourly_forecast/forecast["+i+"]/humidity", source16);


                      }
                      catch (Exception e) {
                      e.printStackTrace();
                                  }
                      }

try to get this line to top of your for loop, i'm pretty sure it will solve your problem. 尝试将此行放在for循环的顶部,我很确定它将解决您的问题。 But if it doesn't try to not to use it. 但是,如果它不尝试不使用它。 I mean try to read your string into an inputstream and work with it. 我的意思是尝试将您的字符串读入输入流并使用它。

InputSource source16 = new InputSource(new StringReader(hourly));
for (int i=1;i<=36;i++){
                  try{
                   humidity = xpath.evaluate("/response/hourly_forecast/forecast["+i+"]/humidity", source16);


                  }
                  catch (Exception e) {
                  e.printStackTrace();
                              }
                  }

As we know string proccess can be a torture if you don't be more careful. 我们知道,如果您不小心的话,字符串处理可能会成为一种折磨。

I had the same trouble when I was doing a swing program with Java SE. 当我用Java SE编写一个swing程序时,我遇到了同样的麻烦。 I had to call xpath.evalute method about 12 different times, each time taking atleast 2-3 seconds which made my program awfully slow. 我不得不调用xpath.evalute方法大约12次,每次至少花费2-3秒,这使我的程序非常慢。

The simple solution was to use a substring. 简单的解决方案是使用子字符串。 It might sound naive and a lot of work, but it actually works if you just have under 10 different parameters to parse from the XML and most importantly the XML is reliable and well formatted. 这听起来似乎很幼稚,但需要大量工作,但是如果您要从XML解析10个以下不同的参数,并且最重要的是XML是可靠且格式正确的,那么它实际上是可行的。

In your case with just 8 different parameters to parse in the entire document, this method is quite easily achievable. 如果您只需要在整个文档中解析8个不同的参数,则此方法非常容易实现。 Just use a for-loop too. 只需使用for循环。

Implement something like this : First, parse your entire XML document to a string. 实现这样的事情:首先,将整个XML文档解析为一个字符串。 Let's call it yourXmlDoc. 我们称之为yourXmlDoc。 And store the values of humidity in a String array. 并将湿度值存储在String数组中。

num1=0;
num2=0;
num3=0;
for (int i=0;i<*number of times you need to repeat*;i++){
num1=yourXmlDoc.indexOf("humidity",num3);
num2=yourXmlDoc.indexOf("/humidity",num1);
num3=num2;
humidity[i]=yourXmlDoc.substring(num1+9,num2-1);
System.out.println(humidity[i]);

.. continue with more parameters to parse and let its starting point be num3
}

PS This is possible only in case of a well formatted XML. PS仅在格式正确的XML情况下才有可能。 Otherwise its very hard to implement and is a waste of time. 否则,它很难实施并且浪费时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM