Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:14:07+00:00 2026-05-28T16:14:07+00:00

My code is too slow, but I’m not sure how to improve it. Reading

  • 0

My code is too slow, but I’m not sure how to improve it. Reading from disk into DOM for a 1k-file takes about 20 ms, that might be okay depending on the disk, but then I’ve got another 20 ms for working on a xpath statement, which is far too much. Here is some sample code with time comments. How can I improve the code?

This happens at construction time:

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = this.dbFactory.newDocumentBuilder(); 
XPathExpression[] ex = new XPathExpression[]{about 30 different expressions}
XPathExpression mainEx =xPath.compile("/rootElement/firstLevel/secondLevel");

Then the code:

Document doc = this.dBuilder.parse("somefile.xml");
//took 20 ms until here
NodeList nodes = (NodeList) mainEx .evaluate,doc, XPathConstants.NODESET);
 //took another 20 ms until here !!!
    for (int i = 0; i < nodes.getLength(); i++) {
    Node n = nodes.item(i);
    for (XPathExpression e:ex) {
         String v = (String) e.evaluate(n, XPathConstants.STRING);
        if (v != null) {
            System.out.println(v);
        }
    }
    }
    //this only takes 5 ms
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:14:08+00:00Added an answer on May 28, 2026 at 4:14 pm

    You’re probably suffering from this problem that I documented here:

    Java XPath (Apache JAXP implementation) performance

    Essentially, you should add these JVM arguments to heavily speed up Xalan’s XPath implementation:

    -Dorg.apache.xml.dtm.DTMManager=
      org.apache.xml.dtm.ref.DTMManagerDefault
    

    or

    -Dcom.sun.org.apache.xml.internal.dtm.DTMManager=
      com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing some Wave file handling and have them read from disk into an
I have write some code but my program is too slow. The problem is
The code compiles without too much complaint, but the last step fails with the
I've been reading StackOverflow too much and started doubting all the code I've ever
I've solved Polygon problem problem at interviewstreet, but it seems too slow. What is
am using system.net.mail to send email as shown below, but its too slow. it
My app is too slow in responding, I guess I'm not loading images Asynchronously,
not sure if the subject entirely conveys what I'm trying to achieve, but let
i'm making a listview ,but scrolling is too slow. i have a ImageButton and
In Antlrworks I get this error: [18:21:03] Checking Grammar Grammar.g... [18:21:26] Grammar.java:12: code too

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.