BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java XML parser – Complete code

By Chaitanya Singh | Filed Under: JAVA Guide

This below code can be used to parse XML strings. The parsed content is arranged in the form of a Java Hashtable. Following is the code used for parsing. It requires the IBM supplied “xerces.jar” and “parser.jar“. The parsed content is arranged in the form of a Java Hashtable. To learn more about Java and XML, you can refer my previous article about Java and XML.

//XML classes to be imported
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.dom.DocumentImpl;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
//Standard Java classes to be imported
import java.util.Vector;
import java.util.Hashtable;
import java.io.ByteArrayInputStream;
public class XmlParser
{
private XmlParser()
{
}
/**
* readXml. Converts XML string into Message object
*
* @param String The XML String to be parsed
* @param String The root tag in the string
* @return Vector A vector of hashtables. Each hashtable is a
* list of key value pairs as seen in the XML
*/
public static Vector readXml (String xmlString, String rootTag)
throws Exception
{
Vector messages = new Vector();
try
{
Document doc = getDocument (xmlString);
messages = getMessages (doc, rootTag);
}
catch (NullPointerException e)
{
throw new Exception ("readXml: No data in XML");
}
catch (Exception e)
{
throw e;
}
return messages;
}
/**
* getDocument. Retrieves the Document representing the XML
*
* @param String
* @return Document
*/
private static Document getDocument (String xmlString)
throws Exception
{
Document doc = new org.apache.xerces.dom.DocumentImpl();
try
{
ByteArrayInputStream xmlByteArray =
new ByteArrayInputStream (xmlString.getBytes());
InputSource xmlInput = new InputSource (xmlByteArray);
DOMParser ps = new DOMParser();
ps.parse (xmlInput);
doc = ps.getDocument();
}
catch (Exception e) {
throw new Exception ("getDocument: Parsing Error");
}
return doc;
}
/*
* This private method parses the XML document and converts it
* into a Vector containing Hashtables of messages
* It calls the private method parseNode to get the Hashtables
*/
private static Vector getMessages (Document doc, String rootTag)
throws Exception
{
Vector messages = new Vector();
try {
NodeList rootList = doc.getElementsByTagName (rootTag);
if (rootList.getLength() != 1)
{
throw new Exception ("getMessages: Invalid XML");
}
Node root = rootList.item(0);
Node parent = root.getParentNode();
if (parent == null || parent.getNodeType() != Node.DOCUMENT_NODE)
{
throw new Exception ("getMessages: Invalid XML");
}
NodeList children = root.getChildNodes();
int childSize = children.getLength();
for (int childIndex = 0; childIndex < childSize; childIndex++)
{
Node child = children.item (childIndex);
if (child.getNodeType() == Node.ELEMENT_NODE)
{
Hashtable messageHtb = parseNode (child);
messages.addElement (messageHtb);
}
}
}
catch (Exception e)
{
throw new Exception ("getMessages: Parsing Error");
}
return messages;
}
/*
* This private method converts all data in the node to a Hashtable
*/
private static Hashtable parseNode (Node messageNode)
throws Exception
{
Hashtable messageHtb = new Hashtable();
String removeColumn = null;
try
{
NodeList columns = messageNode.getChildNodes();
int noOfColumns = columns.getLength();
String columnName = null, columnData = null;
int dataCounter = 0;
for (int index = 0; index < noOfColumns; index++)
{
Node node = columns.item (index);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element column = (Element) node;
columnName = column.getNodeName();
NodeList dataNodes = column.getChildNodes();
int dataTexts = dataNodes.getLength();
for (int dataIndex=0; dataIndex<dataTexts; dataIndex++)
{
Node dataNode = dataNodes.item (dataIndex);
if (dataNode.getNodeType() == Node.TEXT_NODE)
{
columnData = dataNode.getNodeValue().trim();
if (columnData.equals("") ||
columnData.equals("r"))
{
continue;
}
else
{
Object colData = messageHtb.get (columnName);
if (colData == null)
{
if (dataCounter != 0)
{
dataCounter = 0;
messageHtb.remove (removeColumn);
removeColumn = null;
}
messageHtb.put (columnName, columnData);
}
else
{
removeColumn = columnName;
dataCounter ++;
String colName = columnName +
String.valueOf (dataCounter);
if (dataCounter == 1)
{
messageHtb.put (colName, colData);
dataCounter ++;
colName = columnName +
String.valueOf (dataCounter);
messageHtb.put (colName, columnData);
}
else
{
messageHtb.put (colName, columnData);
}
} // End - colData = null
} // End - columnData = blank
} // End - dataNode = Node.TEXT_NODE
} // End - Loop for all children in a node
} // End - If node is Element
} // End - Loop for all columns
}
catch (Exception e)
{
throw new Exception ("parseNode: Parsing Error");
}
if (removeColumn != null)
{
messageHtb.remove (removeColumn);
}
return messageHtb;
}
}

Search Keywords: IBM XML4J, Java, XML Parser.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap