Tuesday, September 13, 2011

Utility for Evaluating XML string,Files in JAVA

Given below is a Java utility class that can be used to evaluate/parse XML files. There are three different methods which parses the given XML file or XML String.

getNodeValueListFromFile

This method can be used to get NodeValue List from the ML file given as String.

getNodeValue

This method can be used to get value of the Node from give XML file.

printNodeValueFromFile

This method prints the Node value from XML file given as string

/**
* @author bacharya
*/
public class XmlUtils {
/**
* Gets Node Value from given XML document with file Path
*
* @param parentTag
* @param tagName
* @param layoutFile
* @return
*/
public static String getNodeValue(String parentTag, String tagName,
String layoutFile) {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = null;
Document doc = null;

try {
docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse(new File(layoutFile));
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("Could not load file");
}
NodeList layoutList = doc.getElementsByTagName(parentTag);

for (int s = 0; s < layoutList.getLength(); s++) {
Node firstPersonNode = layoutList.item(s);
if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
Element firstPersonElement = (Element) firstPersonNode;
NodeList firstNameList = (firstPersonElement)
.getElementsByTagName(tagName);
Element firstNameElement = (Element) firstNameList.item(0);
NodeList textFNList = (firstNameElement).getChildNodes();
return textFNList.item(0).getNodeValue().trim();
}
}
return null;
}

/**
* Gets Node Value from given XML as String
*
* @param parentTag
* @param tagName
* @param xmlRecords
* @return
*/
public static void printNodeValueFromFile(String parentTag, String tagName,
String xmlRecords) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));

Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName(parentTag);

for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);

NodeList name = element.getElementsByTagName(tagName);
Element line = (Element) name.item(0);
System.out.println(": " + getCharacterDataFromElement(line));
}
} catch (Exception e) {
e.printStackTrace();
}
return "";

}

/**
* Gets Node Value from given XML as Map of List of Strings
*
* @param parentTag
* @param tagName
* @param xmlRecords
* @return
*/
public static Map<String, List<String>> getNodeValueListFromFile(
String parentTag, String tagName, String xmlRecords) {
Map<String, List<String>> nodeMapList = new HashMap<String, List<String>>();
List<String> valueList = new ArrayList<String>();

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));

Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName(parentTag);

for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);

NodeList name = element.getElementsByTagName(tagName);
Element line = (Element) name.item(0);
valueList.add(getCharacterDataFromElement(line));
}
} catch (Exception e) {
e.printStackTrace();
}
nodeMapList.put(tagName, valueList);
return nodeMapList;

}

public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "?";
}

/**
* Unit test for XmlUtils class
*
* @param args
*/
public static void main(String args[]) {
String rec = "<Layout>" + " <Data>" + " <Name>testLayout</Name>"
+ " <Delimiter>s</Delimiter>" + " </Data>" + " <Details>"
+ " <DataElement>" + " <fieldName>GROUP</fieldName>"
+ " <Type>String</Type>" + " <Location>" + " <Num>1</Num>"
+ " </Location>" + " </DataElement>" + " <DataElement>"
+ " <fieldName>ENTITY_CODE</fieldName>"
+ " <Type>String</Type>" + " <Location>" + " <Num>2</Num>"
+ " </Location>" + " </DataElement>" + " </Details>"
+ "</Layout>";

Map<String, List<String>> nodeMapList = XmlUtils
.getNodeValueListFromFile("DataElement", "fieldName", rec);
Map<String, List<String>> nodeMapList1 = XmlUtils
.getNodeValueListFromFile("DataElement", "Type", rec);
Map<String, List<String>> nodeMapList2 = XmlUtils
.getNodeValueListFromFile("Location", "Num", rec);

List<String> val = nodeMapList.get("fieldName");

System.out.println(val.size());
for (int i = 0; i < val.size(); i++) {
System.out.println(nodeMapList1.get("Type").get(i));
System.out.println(nodeMapList2.get("Num").get(i));
}

System.out.println(
XmlUtils.getNodeValueFromFile("DataElement", "fieldName",
rec));

}
}



Test Output :

2
String
1
String
2
: GROUP
: ENTITY_CODE

No comments: