Log in
updated 11:29 AM UTC, May 4, 2016

Ways to increase the performance of XML processing in Java.

  • Written by Madhu V Rao
  • Published in Core Java

Ways to increase the performance of XML processing in Java.

I dont go into the details of which is better SAX parser or DOM parser.
We have used DOM parser as we wanted the performance to be higher and we didnt really care about the memory consumption.

In this article I will explain ways in which performance can be improved so that your xml processing can be quick.


Case 1 )

If you referring anĀ  XML from another XML , there are chances that you will be parsing same file several times.
In this scenario , make sure that you save the parsed XML file in a hashmap and use it again if the file is referred again


Check below snippet:

HashMap parsedXMLFiles = new HashMap();

public void yourMethodToParsetheFiles(String fileName) 
{
	//	your logic
	Node parsedNode = null;
	if (parsedXMLFiles.containsKey(fileName))
	{
                parsedNode = parsedXMLFiles.get(fileName);
	}
        else
	{
                parsedNode = parse(fileName);
                parsedXMLFiles.put(fileName, parsedNode);
	}
}


public Node parse(String xmlFileName)
{
        DocumentBuilder parser;
        try
        {
        	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        	parser = dbf.newDocumentBuilder();
              	document = parser.parse(xmlFileName);
        }
        catch (ParserConfigurationException ex)
        {
        	//handle exceotions
        }
        catch (SAXException ex)
        {
        	//handle exceotions        
        }
        catch (IOException ex)
        {
        	//handle exceotions           
        }
        root = document.getDocumentElement();
        return root;
}

Case 2)

If you are using XPath's in your XML ,most probably you will be using following XPATH API's :

String evalAndSendTextValueOfXPath(String xPath, Node rootNode)
{
	XPathFactory factory = XPathFactory.newInstance();
	XPath xpathInstance = factory.newXPath();
	XPathExpression expr = xpathInstance.compile(xPath);
	// Evaluate
	Object result = expr.evaluate(rootNode, XPathConstants.NODESET);
	NodeList nodes = (NodeList) result;
	if (nodes != null && nodes.getLength() == 1) 
	{
		return nodes.item(0).getTextContent();
	}
	return null;
}

This snippet of code is expensive since a number of objects must be created for each evaluation.

Instead of the above code snippet , its better to use CachedXpathAPI's which is faster by 100X than the XPathApi's simple reason is it caches some of the objects created.

more information about this Api is provided here :
http://xml.apache.org/xalan-j/apidocs/org/apache/xpath/CachedXPathAPI.html

These CachedXPathAPI's is helpful only when reused, and hence it is created once and is re-used to evaluate the Xpath's.

Please make sure you increase your JVM's heap size to 1500M or higher to avoid outOfMemoryExceptions.

//create this outside the method once
static CachedXpathAPI cachedXPathAPI = new CachedXPathAPI();

String evalAndSendTextValueOfXpath(String xPath, Node rootNode)
{
	NodeIterator iterator = null;
	try
        	{
	            iterator = cachedXPathAPI.selectNodeIterator(rootNode, xPath);
	}
	catch (TransformerException ex)
	{
		//Exception handling
         	}
	Node node = null;
	if ((node = iterator.nextNode()) != null)
         	{
		return node.getTextContent();
	}
	return null;
}

Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest