0
DOMを使用して複雑なXMLファイルを解析するにはどうすればよいですか?私は学年の各子供にアクセスする必要がありますが、私はクラス、子、学生、教師の要素にアクセスするには、XMLファイル内のすべてのクラスを取得しています。DOMを使用して複雑なXMLファイルを解析するにはどうすればよいですか?
public SchoolM readFileNBuildModel(String filePath) {
File file = new File(filePath);
if (file.exists()) {
AppLauncher.getLog().log(Level.INFO, " File Exist : " + filePath,filePath);
try {
AppLauncher.getLog().log(Level.INFO, " Parsing File : " + filePath,filePath);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder;
documentBuilder = dbf.newDocumentBuilder();
Document doc = documentBuilder.parse(file);
doc.getDocumentElement().normalize();
Element schoolNode = doc.getDocumentElement();
NodeList gradeList = doc.getElementsByTagName("grade");
SortedSet<GradeM> gradeSet = new TreeSet<GradeM>();
for (int temp = 0; temp < gradeList.getLength(); temp++) {
Node gradeNode = gradeList.item(temp);
Element gradeElemet = (Element) gradeNode;
GradeM gradeM = new GradeM(gradeElemet.getAttribute("id"));
SortedSet<ClassM> classSet = new TreeSet<ClassM>();
NodeList classList = doc.getElementsByTagName("classroom");
for (int classIndex = 0; classIndex < classList.getLength(); classIndex++) {
Node classNode = classList.item(classIndex);
Element classElement = (Element) classNode;
ClassM classM = new ClassM(classElement.getAttribute(CSVColumnAttributeEnum.CLASSROOM_ID.getXmlMapColumnName()),
classElement.getAttribute(CSVColumnAttributeEnum.CLASSROOM_NAME.getXmlMapColumnName()));
SortedSet<TeacherM> teacherSet = new TreeSet<TeacherM>();
SortedSet<StudentM> studentSet = new TreeSet<StudentM>();
NodeList teacherList = doc.getElementsByTagName("teacher");
NodeList studentList = doc.getElementsByTagName("student");
for (int studentIndex = 0; studentIndex < studentList.getLength(); studentIndex++) {
Node studentNode = studentList.item(studentIndex);
Element studentElement = (Element) studentNode;
if(studentElement != null){
StudentM studentM = new StudentM(studentElement.getAttribute(CSVColumnAttributeEnum.STUDENT_ID.getXmlMapColumnName()),
studentElement.getAttribute(CSVColumnAttributeEnum.STUDENT_FIRST_NAME.getXmlMapColumnName()), studentElement.getAttribute(CSVColumnAttributeEnum.STUDENT_LAST_NAME.getXmlMapColumnName()),
studentElement.getAttribute(CSVColumnAttributeEnum.STUDENT_GRADE.getXmlMapColumnName()));
studentSet.add(studentM);
}
}
for (int teacherIndex = 0; teacherIndex < teacherList.getLength(); teacherIndex++) {
Node teacherNode = teacherList.item(classIndex);
Element teacherElement = (Element) teacherNode;
if(teacherElement != null){
TeacherM teacherM = new TeacherM(teacherElement.getAttribute(CSVColumnAttributeEnum.TEACHER1_ID.getXmlMapColumnName()),
teacherElement.getAttribute(CSVColumnAttributeEnum.TEACHER1_LAST_NAME.getXmlMapColumnName()), teacherElement.getAttribute(CSVColumnAttributeEnum.TEACHER1_FIRST_NAME.getXmlMapColumnName()));
teacherSet.add(teacherM);
}
}
classM.setStudentSet(studentSet);
classM.setTeacherSet(teacherSet);
classSet.add(classM);
}
gradeM.setClassSet(classSet);
gradeSet.add(gradeM);
}
SchoolM schoolM = new SchoolM(schoolNode.getAttribute("id"), schoolNode.getAttribute("schoolName"),gradeSet);
return schoolM;
} catch (ParserConfigurationException e) {
AppLauncher.getLog().log(Level.SEVERE, " File Conversion failed because of : /n" + e.toString());
} catch (SAXException e) {
AppLauncher.getLog().log(Level.SEVERE, " File Conversion failed because of : /n" + e.toString());
} catch (IOException e) {
AppLauncher.getLog().log(Level.SEVERE, " File Conversion failed because of : /n" + e.toString());
}
}else{
AppLauncher.getLog().log(Level.WARNING, " File Does Not Exist : " + filePath,filePath);
}
return new SchoolM();
XMLファイル:
<grade id="1">
<classroom id="101" name="Mrs. Jones' Math Class">
<teacher id="10100000001" first_name="Barbara" last_name="Jones"/>
<student id="10100000010" first_name="Michael" last_name="Gil"/>
<student id="10100000011" first_name="Kimberly" last_name="Gutierrez"/>
<student id="10100000013" first_name="Toby" last_name="Mercado"/>
<student id="10100000014" first_name="Lizzie" last_name="Garcia"/>
<student id="10100000015" first_name="Alex" last_name="Cruz"/>
</classroom>
<classroom id="102" name="Mr. Smith's PhysEd Class">
<teacher id="10200000001" first_name="Arthur" last_name="Smith"/>
<teacher id="10200000011" first_name="John" last_name="Patterson"/>
<student id="10200000010" first_name="Nathaniel" last_name="Smith"/>
<student id="10200000011" first_name="Brandon" last_name="McCrancy"/>
<student id="10200000012" first_name="Elizabeth" last_name="Marco"/>
<student id="10200000013" first_name="Erica" last_name="Lanni"/>
<student id="10200000014" first_name="Michael" last_name="Flores"/>
<student id="10200000015" first_name="Jasmin" last_name="Hill"/>
<student id="10200000016" first_name="Brittany" last_name="Perez"/>
<student id="10200000017" first_name="William" last_name="Hiram"/>
<student id="10200000018" first_name="Alexis" last_name="Reginald"/>
<student id="10200000019" first_name="Matthew" last_name="Gayle"/>
</classroom>
<classroom id="103" name="Brian's Homeroom">
<teacher id="10300000001" first_name="Brian" last_name="O'Donnell"/>
</classroom>
</grade>