我目前有一些xml:
<Schools>
<School SchoolName="name1" ... >
<Students>
<Student studentName="student1" .../>
<Student studentName="student2" .../>
</Stuidents>
</School>
<School SchoolName="name2" ...>
<Students>
<Student studentName="student3" ... />
<Student studentName="student3" ... />
</Students>
</School>
</Schools>
如果我使用 DOM 解析器创建“学校”和“学生”的节点列表,在学生节点列表中,我如何能够从学生那里获取 SchoolName,以便能够保存在数据库/学生类中:StudentName,学生详细信息,学校名称。
NodeList schoolNodeList = docEle.getElementsByTagName("School");
NodeList studentNodeList = docEle.getElementsByTagName("Student");
parseStudentNodeList(studentNodeList);
private void parseStudentNodeList(NodeList studentsNodeList)
{
Student student = new Student();
for (int i = 0; i < studentNodeList.getLength(); i++)
{
Element el (Element) studentNodeList.item(i);
student.setName(el.getAttribute("studentName"));
student.setSchool( /* Help needed here */ );
}
}
最好不要将“SchoolName”变量添加到学生部分。