Created
November 26, 2025 05:55
-
-
Save untainsYD/33cf0aff909c1f890da80f7223220bc6 to your computer and use it in GitHub Desktop.
Java advanced: Laboratory 3, Task5: class StudentSaxHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ua.inf.iwanoff.java.advanced.lab3.service; | |
| import org.xml.sax.Attributes; | |
| import org.xml.sax.SAXException; | |
| import org.xml.sax.helpers.DefaultHandler; | |
| public class StudentSaxHandler extends DefaultHandler { | |
| // Прапорець, що вказує, чи ми всередині тегу, текст якого треба вивести | |
| private boolean isPrintableElement = false; | |
| private String currentElement = ""; | |
| @Override | |
| public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { | |
| currentElement = qName; | |
| if (qName.equals("Group")) { | |
| System.out.println("Знайдено групу."); | |
| } else if (qName.equals("Student")) { | |
| String id = attributes.getValue("id"); | |
| System.out.println(" -----------------------"); | |
| System.out.println(" Студент (ID: " + id + "):"); | |
| } else { | |
| // Якщо це теги з даними (Name, Age, Grade, GroupName), ставимо прапорець | |
| isPrintableElement = true; | |
| } | |
| } | |
| @Override | |
| public void endElement(String uri, String localName, String qName) throws SAXException { | |
| isPrintableElement = false; | |
| if (qName.equals("Group")) { | |
| System.out.println("Кінець обробки групи."); | |
| } | |
| } | |
| @Override | |
| public void characters(char[] ch, int start, int length) throws SAXException { | |
| if (isPrintableElement) { | |
| String data = new String(ch, start, length).trim(); | |
| if (!data.isEmpty()) { | |
| System.out.println(" " + currentElement + ": " + data); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment