I am trying to read XML into a HashMap
, I am trying to read each XML node and create new object based on its contents.
How do I retrieve the value of each node of the XML?
For example : I need to retrieve birthdate
value within my for loop and use it to create a new studentInfo
object.
xml :
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="students.xsd"><description>A bunch students and courses</description><student studentID="0144085" gender ="M"><firstname>Jack</firstname>
<lastname>Blogs</lastname> <birthday day="21" month="04" year="1983"/><paper>Data Structures and Algorithms</paper><paper>Distributed and Mobile Systems</paper> <paper>Software Engineering</paper><paper>Highly Secure Systems</paper><paper>Engineering Computations</paper><paper>Object Oriented Programming</paper></student>
code :
//jf : set StudentINfoSet class properties
this.description = rootXMLNode.getElementsByTagName( "description" ).item( 0 ).getTextContent();
studentMap = new HashMap<String, StudentInfo>();
NodeList nodeList = document.getElementsByTagName( "student" );
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
StudentInfo si = new StudentInfo(Integer.toString(i)){};
/*
* String studentID, String firstName, String lastName,
String birthdate, String gender, char studentGender
* */
this.studentMap.put(Integer.toString(i), si);
}
System.out.println("Number of students : "+nodeList.getLength());
I would use JAXB, see example here http://www.mkyong.com/java/jaxb-hello-world-example/