XML

"Continuous effort - not strength or intelligence - is the key to unlocking our potential." ==> Winston Churchill

Name:
Location: Bangalore, India

Sunday, February 17, 2008

Accessing an XML document using DOM

Requirement:
We have a completely organized XML file with SQL Queries. We need to parse the XML file and retrieve only the SQL Queries based on module code and sql query id.


XML File (ABCSQLQueries):


<SQL_ROOT>
<MODULE CODE="REQUEST">
<SQL_SEQMENT ID="PAYMENTTERMS_QUERY">
SELECT A.PAYMENTTERMSKEY, A.DISPLAY FROM OWN_ABC.PAYMENTTERMS A, OWN_ABC.REQUESTDETAILS B WHERE
B.REQUESTDETAILSKEY = ? AND A.PAYMENTTERMSKEY = B.PAYMENTTERMSKEY
</SQL_SEQMENT>
<SQL_SEQMENT ID="REQUEST_UPDATE">
update request query
</SQL_SEQMENT>
</MODULE>
<MODULE CODE="BANK">
<SQL_SEQMENT ID="BANK_SELECT">
Select bank query
</SQL_SEQMENT>
<SQL_SEQMENT ID="BANK_UPDATE">
update bank query
</SQL_SEQMENT>
</MODULE>
<MODULE CODE="COUNTRY">
<SQL_SEQMENT ID="COUNTRY_SELECT">
Select country query
</SQL_SEQMENT>
<SQL_SEQMENT ID="COUNTRY_UPDATE">
update country query
</SQL_SEQMENT>
</MODULE>
</SQL_ROOT>

Code:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Attr;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.DOMException;

import org.xml.sax.SAXException;

import java.io.InputStream;
import java.io.IOException;

public class XMLParsing {

static Document doc;
static Element element;
private static String fileName = "ABCSQLQueries.xml";

public static void main(String[] args) {
XMLParsing xr = new XMLParsing();
xr.getXmlParser();
}

public void getXmlParser() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try {
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = this.getClass().getResourceAsStream(fileName);
doc = builder.parse(is);
}catch(ParserConfigurationException parserException) {
}catch(SAXException saxException) {
}catch(IOException ioException) {
}catch(Exception exception){}
getElements("REQUEST", "PAYMENTTERMS_QUERY");
}

public static String getElements(String moduleName, String queryName) {

String gtemQuery = null;
// Retrive the entire Document from the Dom Tree
element = doc.getDocumentElement();
String s1 = element.getTagName();
System.out.println("\nRoot Tag Name: " + s1);

NodeList parentNodeList = element.getElementsByTagName("MODULE");

for(int parentNode = 0; parentNode < parentNodeList.getLength(); parentNode++) {
Node queryNode = parentNodeList.item(parentNode);

String attributeValue = getAttributes(queryNode);

if(queryNode.getNodeName().equals("MODULE") && attributeValue.equals(moduleName)) {

NodeList childNodeList = queryNode.getChildNodes();

for(int i=0; i<childNodeList.getLength(); i++) {
String childAttributeValue = null;

Node childNode = childNodeList.item(i);

if(childNode.getNodeType() == Node.ELEMENT_NODE){
childAttributeValue = getAttributes(childNode);

if(childAttributeValue.trim().equals(queryName)){
System.out.println("Query: " + childNode.getTextContent().trim());
gtemQuery = childNode.getTextContent().trim();
break;
}
}
}
}
}
return gtemQuery;
}

public static String getAttributes(Node firstLevelNode) {

String attributeValue = null;

NamedNodeMap nodeMap = firstLevelNode.getAttributes();

for (int i=0; i<nodeMap.getLength(); i++) {
Node attributeNode = nodeMap.item(i);
// Get attribute value
attributeValue = attributeNode.getNodeValue();
System.out.println("Attribute Value: " + attributeValue );
}
return attributeValue;
}

}

0 Comments:

Post a Comment

<< Home