简体   繁体   中英

Parsing a xml, exponential increase in time

I have a parser which parses and collects the requires fields and constructs a object out of it. Suppose if xml is like below

<xml>
<p1>
...
...
</p1>
<p2>
...
</p2>
...
...
</xml>

My java code parses it and code seems like below.

for each product //p1,p2 etc..
 print start time
 parse that node, which returns a object
 print end time
 add the object to list.

The sample code is below

products = (NodeList) xPath.evaluate("/xml/product",pxml,XPathConstants.NODESET);
for (int i = 0; i < products.getLength(); i++)
            {
                System.out.println("parsing product ::"+i+":" + (System.currentTimeMillis()-time));
                BookDataInfo _parsedPoduct = ParseProduct(products.item(i));
                System.out.println("parsing product finished ::"+i+":" + (System.currentTimeMillis()-time));
                if (_parsedPoduct.getParsingSucceeded())
                {
                    pparsedProducts.add(_parsedPoduct);
                }
            }

I have printed the times before parsing the node and after that, the time is exponentially increasing with no.of products like for 1st product takes 100ms where as some 300th product takes 2000ms. In each case same part of code is executed for parsing. Could any one have idea why it happens?

I can't post the code what parseproduct is doing but found out where the time is consumed most.

private NodeList getNodelist(Node xml, String Name)
{
    long time = System.currentTimeMillis();
    System.out.println("Nodelist start::" + (System.currentTimeMillis() - time));
    NodeList nodes = (NodeList)xPath.evaluate(Name,xml,XPathConstants.NODESET);
    System.out.println("Nodelist end::" + (System.currentTimeMillis() - time));
    return nodes;
}

similarly for getting node value at a stmt Node node = (Node)xPath.evaluate(Name,xml,XPathConstants.NODE);

here xPath is a static object of type XPath. when multiple times the above function is called for a product, the later calls are taking much time, like in start it took 2/3 ms but later(say product 300) it took 55-60ms for each call.

May I am missing some thing here? Thanks!

查看DOM和SAX解析之间的区别,DOM允许您查询XML文件,但为此必须将整个文档上传到内存,如果您只想创建对象,则最好使用SAX解析器

The problem is solved. The main issue is the one mentioned in below link. XPath.evaluate performance slows down (absurdly) over multiple calls

Followed the steps mentioned in that, it drastically reduced the time consumed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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