Is it possible to give path expressions in SAX parser? I have an XML file which has a few same name tags, but they are in different element. Is there any way to differentiate 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 differentiate between the Name and Id of a student from the name and ID of a school.
Thanks for the response:
I have created a student pojo which has the following fields- school_id,school_name, student_id and student_name and getter and setter methods for them. This is my temporary parser implementation. When i parse the xml, I need to put the values of school name, id , student name, id in the pojo and return it. Can you tell me on how I should implement the stack for the differentiation. This is my parser framework::
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);
}
}
Key Difference of DOM and SAX DOM stands for Document Object Model while SAX stands for Simple API for XML parsing. DOM parser load full XML file in-memory and creates a tree representation of XML document, while SAX is an event based XML parser and doesn't load whole XML document into memory.
SAX parser scrutinizes an XML file character by character, and translates the XML file into a series of events, such as startElement(), endElement() and characters(). A ContentHandler object will process these events to perform the appropriate action.
SAX (Simple API for XML) is an event-driven algorithm for parsing XML documents. SAX is an alternative to the Document Object Model (DOM). Where the DOM reads the whole document to operate on XML, SAX parsers read XML node by node, issuing parsing events while making a step through the input stream.
1)SAX is an event-driven push model for processing XML. It is not a W3C standard, 2)Rather than building a tree representation of an entire document as DOM does, a SAX parser fires off a series of events as it reads through the document.
In a SAX parser you are given each element in document order. You have to maintain a stack to track nesting (push onto the stack when handling startElement, and pop for endElement). You can differentiate the different <Name>
elements by what is currently on the stack.
Alternatively, just keep a variable that tells you if you've encountered a <School>
tag or <Student>
tag to tell you which type of <Name>
you are seeing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With