我正在尝试将 XML 读入HashMap
,我正在尝试读取每个 XML 节点并根据其内容创建新对象。
如何检索 XML 的每个节点的值?
例如:我需要birthdate
在我的 for 循环中检索值并使用它来创建一个新studentInfo
对象。
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>
代码 :
//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());