So I spent a while this morning creating an xml parser in java which is part of a job interview. I would love to have some people tear it apart so I can learn from any mistakes that I made so I can grow for future job interviews.
XML file:
<Employees>
<Employee>
<Name> First Last</Name>
<ID> 00001 </ID>
</Employee>
<Employee>
<Name> First2 Last</Name>
<ID> 00002 </ID>
</Employee>
<Employee>
<Name> First3 Last</Name>
<ID> 00003 </ID>
</Employee>
</Employees>
Java File (126 LOC)-
//@Author HunderingThooves
import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class Main {
static class openXML{
private int employeeNumber;
private String employeeName;
void getEmployee(int i){
String[] xmlStr = xmlLoader(i);
setEmployeeName(xmlStr[0]);
setEmployeeNumber(xmlStr[1]);
}
void setEmployeeNumber(String s){
employeeNumber = Integer.parseInt(s);
}
void setEmployeeName(String s){
employeeName = s;
}
String getEmployeeName(){
return employeeName;
}
int getEmployeeNumber(){
return employeeNumber;
}
};
public static final String[] xmlLoader(int i){
String xmlData[] = new String[2];
try {
int employeeCounter = i;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("test.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
NodeList listOfEmployee = doc.getElementsByTagName("Employee");
Node firstEmployeeNode = listOfEmployee.item(employeeCounter-1);
int totalEmployees = listOfEmployee.getLength();
//Break xml file into parts, then break those parts down int an array by passing individual elements to srtings
if(firstEmployeeNode.getNodeType() == Node.ELEMENT_NODE){
Element firstEmployeeElement = (Element)firstEmployeeNode;
//-------
NodeList nameList = firstEmployeeElement.getElementsByTagName("Name");
Element nameElement = (Element)nameList.item(0);
NodeList textNameList = nameElement.getChildNodes();
xmlData[0]= (((Node)textNameList.item(0)).getNodeValue().trim()).toString();
//-------
NodeList IDList = firstEmployeeElement.getElementsByTagName("ID");
Element IDElement = (Element)IDList.item(0);
NodeList textIDList = IDElement.getChildNodes();
xmlData[1]= (((Node)textIDList.item(0)).getNodeValue().trim()).toString();
//------
}//
}
catch(NullPointerException npe){
System.out.println("The employee number you searched for is incorrect or does not yet exist, try again. ");
String s[] = {" ", " "};
main(s);
}
catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}
catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}
catch (Throwable t) {
t.printStackTrace ();
}
return xmlData;
}
public static void main(String args[]){
openXML myXmlHandler = new openXML();
Scanner input = new Scanner(System.in);
int i = -1;
do{
try{
String s = "";
System.out.println("Enter the employee number that you're searching for: ");
s = input.next();
try{
i= Integer.parseInt(s);
} catch(NumberFormatException nfe){ i = -1;}
} catch(Exception e){System.out.println("Error: " +e.getMessage());}
}while(i <= 0);
myXmlHandler.getEmployee(i);
System.out.println("The employee Name is: " + myXmlHandler.getEmployeeName());
System.out.println("The employee Number is: " + myXmlHandler.getEmployeeNumber());
System.exit(0);
}
}
There is one known issue in the file that I will not mention unless someone finds it, I spent about an hour trying to pin it down and the best I could do is sweep it under the rug.