Is it possible to give path expressions in a SAX parser? I have an XML file with several name tags, but they are in a different element. Is there a way to distinguish between them. Here is the XML:
<Schools>
<School>
<ID>335823</ID>
<Name>Fairfax High School</Name>
<Student>
<ID>4195653</ID>
<Name>Will Turner</Name>
</Student>
<Student>
<ID>4195654</ID>
<Name>Bruce Paltrow</Name>
</Student>
<Student>
<ID>4195655</ID>
<Name>Santosh Gowswami</Name>
</Student>
</School>
<School>
<ID>335824</ID>
<Name>FallsChurch High School</Name>
<Student>
<ID>4153</ID>
<Name>John Singer</Name>
</Student>
<Student>
<ID>4154</ID>
<Name>Shane Warne</Name>
</Student>
<Student>
<ID>4155</ID>
<Name>Eddie Diaz</Name>
</Student>
</School>
</Schools>
I want to distinguish the name and identifier of the student from the name and identifier of the school.
Thanks for the answer:
I created a student pojo that has the following fields: school_id, school_name, student_id and student_name, as well as the getter and setter methods. This is my temporary implementation of the parser. When I parse the xml, I need to put the values โโof the school name, id, student name, id in pojo and return it. Can you tell me how I should implement the stack for differentiation. This is my parsing:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class HandleXML extends DefaultHandler {
private student info;
private boolean school_id = false;
private boolean school_name = false;
private boolean student_id = false;
private boolean student_name = false;
private boolean student = false;
private boolean school = false;
public HandleXML(student record) {
super();
this.info = record;
school_id = false;
school_name = false;
student_id = false;
student_name = false;
student = false;
school = false;
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("student")) {
student = true;
}
if (qName.equalsIgnoreCase("school")) {
school_id = true;
}
if (qName.equalsIgnoreCase("school_id")) {
school_id = true;
}
if (qName.equalsIgnoreCase("student_id")) {
student_id = true;
}
if (qName.equalsIgnoreCase("school_name")) {
school_name = true;
}
if (qName.equalsIgnoreCase("student_name")) {
student_name = true;
}
}
@Override
public void endElement(String uri, String localName,
String qName)
throws SAXException {
}
@Override
public void characters(char ch[], int start, int length)
throws SAXException {
String data = new String(ch, start, length);
}
}