XPath queries getting slower and slower...

Discussion in 'Java' started by Andre Charbonneau, Feb 15, 2005.

  1. Hi,
    I'm having a problem in my java application with XPath queries (using Sun's
    default XPath API in 1.5.0).

    The problem is the following:
    I have a XML document (given by Ganglia, around 300K in size), which
    contains many "HOST" nodes, each of them containing many "METRIC" nodes.
    Something like:

    <GANGLIA_XML>
    <HOST>
    <METRIC NAME="..." .../>
    <METRIC NAME="..." .../>
    <METRIC NAME="..." .../>
    <METRIC NAME="..." .../>
    ...
    </HOST>

    <HOST>
    <METRIC NAME="..." .../>
    <METRIC NAME="..." .../>
    <METRIC NAME="..." .../>
    <METRIC NAME="..." .../>
    ...
    </HOST>
    ...
    </GANGLIA_XML>


    Now if I get a NodeList of HOST nodes (using XPath.evaluate), and then for
    each of these node get a NodeList of "METRIC" nodes (again using
    XPath.evaluate), then when I iterate through the METRIC node list and
    evaluate each of them to get the "NAME" attribute value (using
    xpath.evaluate("@NAME", doc, ...), the performance of each XPath query to
    do so gets slower and slower for each successive XPath.evaluate call.

    Did anyone experience this behavior before? Any idea why this is happening?

    Here is a snapshot of a little test application that reproduces this
    behavior: (in this test app I simply get all the METRIC nodes right away,
    but the performance problem still happens)

    ---------------------------------
    public class Test
    {
    public static void main(String[] args)
    {
    try
    {
    DocumentBuilder db =
    DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new File("ganglia.output.xml));
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList metricNodes = (NodeList) xpath.evaluate("//METRIC", doc,
    XPathConstants.NODESET);

    for(int i = 0; i < metricNodes.getLength(); i++)
    {
    Node n = metricNodes.item(i);
    long t1 = Calendar.getInstance().getTimeInMillis();
    String s = (String) xpath.evaluate("@NAME", n, XPathConstants.STRING);
    long t2 = Calendar.getInstance().getTimeInMillis();
    System.out.println("time: " + (t2 - t1));
    }
    }
    catch(Exception e)
    {
    System.out.println("Error: " + e);
    }

    System.exit(0);
    }
    }
     
    Andre Charbonneau, Feb 15, 2005
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.