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 3990194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:27:53+00:00 2026-05-20T06:27:53+00:00

For one of our applications, I’ve written a utility that uses java’s DOM parser.

  • 0

For one of our applications, I’ve written a utility that uses java’s DOM parser. It basically takes an XML file, parses it and then processes the data using one of the following methods to actually retrieve the data.

getElementByTagName()
getElementAtIndex()
getFirstChild()
getNextSibling()
getTextContent()

Now i have to do the same thing but i am wondering whether it would be better to use an XSLT stylesheet. The organisation that sends us the XML file keeps changing their schema meaning that we have to change our code to cater for these shema changes. Im not very familiar with XSLT process so im trying to find out whether im better of using XSLT stylesheets rather than “manual parsing”.

The reason XSLT stylesheets looks attractive is that i think that if the schema for the XML file changes i will only need to change the stylesheet? Is this correct?

The other thing i would like to know is which of the two (XSLT transformer or DOM parser) is better performance wise. For the manual option, i just use the DOM parser to parse the xml file. How does the XSLT transformer actually parse the file? Does it include additional overhead compared to manually parsing the xml file? The reason i ask is that performance is important because of the nature of the data i will be processing.

Any advice?

Thanks

Edit

Basically what I am currently doing is parsing an xml file and process the values in some of the xml elements. I don’t transform the xml file into any other format. I just extract some value, extract a row from an Oracle database and save a new row into a different table. The xml file I parse just contains reference values I use to retrieve some data from the database.

Is xslt not suitable in this scenario? Is there a better approach that I can use to avoid code changes if the schema changes?

Edit 2

Apologies for not being clear enough about what i am doing with the XML data. Basically there is an XML file which contains some information. I extract this information from the XML file and use it to retrieve more information from a local database. The data in the xml file is more like reference keys for the data i need in the database. I then take the content i extracted from the XML file plus the content i retrieved from the database using a specific key from the XML file and save that data into another database table.

The problem i have is that i know how to write a DOM parser to extract the information i need from the XML file but i was wondering whether using an XSLT stylesheet was a better option as i wouldnt have to change the code if the schema changes.

Reading the responses below it sounds like XSLT is only used for transorming and XML file to another XML file or some other format. Given that i dont intend to transform the XML file, there is probably no need to add the additional overhead of parsing the XSLT stylesheet as well as the XML file.

  • 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-20T06:27:54+00:00Added an answer on May 20, 2026 at 6:27 am

    I think that what you need is actually an XPath expression. You could configure that expression in some property file or whatever you use to retrieve your setup parameters.

    In this way, you’d just change the XPath expression whenever your customer hides away the info you use in yet another place.

    Basically, an XSLT is an overkill, you just need an XPath expression. A single XPath expression will allow to home in onto each value you are after.

    Update

    Since we are now talking about JDK 1.4 I’ve included below 3 different ways of fetching text in an XML file using XPath. (as simple as possible, no NPE guard fluff I’m afraid 😉

    Starting from the most up to date.

    0. First the sample XML config file

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <param id="MaxThread" desc="MaxThread"        type="int">250</param>
        <param id="rTmo"      desc="RespTimeout (ms)" type="int">5000</param>
    </config>
    

    1. Using JAXP 1.3 standard part of Java SE 5.0

    import javax.xml.parsers.*;
    import javax.xml.xpath.*;
    import org.w3c.dom.Document;
    
    public class TestXPath {
    
        private static final String CFG_FILE = "test.xml" ;
        private static final String XPATH_FOR_PRM_MaxThread = "/config/param[@id='MaxThread']/text()";
        public static void main(String[] args) {
    
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            docFactory.setNamespaceAware(true);
            DocumentBuilder builder;
            try {
                builder = docFactory.newDocumentBuilder();
                Document doc = builder.parse(CFG_FILE);
                XPathExpression expr = XPathFactory.newInstance().newXPath().compile(XPATH_FOR_PRM_MaxThread);
                Object result = expr.evaluate(doc, XPathConstants.NUMBER);
                if ( result instanceof Double ) {
                    System.out.println( ((Double)result).intValue() );
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    2. Using JAXP 1.2 standard part of Java SE 1.4-2

    import javax.xml.parsers.*;
    import org.apache.xpath.XPathAPI;
    import org.w3c.dom.*;
    
    public class TestXPath {
    
        private static final String CFG_FILE = "test.xml" ;
        private static final String XPATH_FOR_PRM_MaxThread = "/config/param[@id='MaxThread']/text()";
    
        public static void main(String[] args) {
    
            try {
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                docFactory.setNamespaceAware(true);
                DocumentBuilder builder = docFactory.newDocumentBuilder();
                Document doc = builder.parse(CFG_FILE);
                Node param = XPathAPI.selectSingleNode( doc, XPATH_FOR_PRM_MaxThread );
                if ( param instanceof Text ) {
                    System.out.println( Integer.decode(((Text)(param)).getNodeValue() ) ); 
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    3. Using JAXP 1.1 standard part of Java SE 1.4 + jdom + jaxen

    You need to add these 2 jars (available from http://www.jdom.org – binaries, jaxen is included).

    import java.io.File;
    import org.jdom.*;
    import org.jdom.input.SAXBuilder;
    import org.jdom.xpath.XPath;
    
    public class TestXPath {
    
        private static final String CFG_FILE = "test.xml" ;
        private static final String XPATH_FOR_PRM_MaxThread = "/config/param[@id='MaxThread']/text()";
    
        public static void main(String[] args) {
            try {
                SAXBuilder sxb = new SAXBuilder();
                Document doc = sxb.build(new File(CFG_FILE));
                Element root = doc.getRootElement();
                XPath xpath = XPath.newInstance(XPATH_FOR_PRM_MaxThread);
                Text param = (Text) xpath.selectSingleNode(root);
                Integer maxThread = Integer.decode( param.getText() );
                System.out.println( maxThread );
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing some maintenance work on one of our old applications that is written
One of our Java applications uses a google API service account to process all
One of our applications is started suddenly terminate with error Can't load file or
I have started upgrading one of our internal software applications, written in ASP.NET Web
I have just re-written the authentication for one of our internal web applications to
One of our applications recently got installed in a system that is tightly locked
We have in one of our applications a single eclipse project that contains all
I am working on a page for one of our applications that displays a
I've had a suspicion that a database connection used in one of our applications
In one of our commercial applications (Win32, written in Delphi) we'd like to implement

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.