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);
    }
    }
    ---------------------------------


    Thanks,
    Andre
    Andre Charbonneau, Feb 15, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thedebugger
    Replies:
    0
    Views:
    336
    thedebugger
    Nov 4, 2004
  2. Ian Roddis

    xslt queries in xml to SQL queries

    Ian Roddis, Feb 26, 2006, in forum: Python
    Replies:
    3
    Views:
    1,445
    Crutcher
    Feb 26, 2006
  3. Richard Tobin
    Replies:
    0
    Views:
    458
    Richard Tobin
    May 2, 2008
  4. yawnmoth

    namespaces and xpath queries

    yawnmoth, Mar 31, 2010, in forum: XML
    Replies:
    3
    Views:
    2,948
    Joe Kesselman
    Mar 31, 2010
  5. Abby Lee

    so many queries within queries I'm confused

    Abby Lee, Aug 4, 2004, in forum: ASP General
    Replies:
    11
    Views:
    335
    Aaron [SQL Server MVP]
    Aug 6, 2004
Loading...

Share This Page