Results 1 to 2 of 2
  1. #1
    eayx is offline Member
    Join Date
    Jun 2008
    Rep Power

    Default how to search xml by SAX

    I used to use DOM and Xpath for search xml file but my xml file have big size.
    I think SAX can solve my problem.
    I have been trying the example java search xml by SAX in google but it don't have that.
    if you have the example source code java for search xml by SAX , please let me know.

  2. #2
    keioGirl is offline Member
    Join Date
    Jul 2008
    Rep Power

    Default SaxParserExample

    Try this. I hope it would help you...
    It's from, but my post count is less than 20 and I can't post links so I copy the java code. :)

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;

    import org.xml.sax.helpers.DefaultHandler;

    public class SaxParserExample extends DefaultHandler{

    List myEmpls;

    private String tempVal;

    //to maintain context
    private Employee tempEmp;

    public SaxParserExample(){
    myEmpls = new ArrayList();

    public void runExample() {

    private void parseDocument() {

    //get a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {

    //get a new instance of parser
    SAXParser sp = spf.newSAXParser();

    //parse the file and also register this class for call backs
    sp.parse("employees.xml", this);

    }catch(SAXException se) {
    }catch(ParserConfigurationException pce) {
    }catch (IOException ie) {

    * Iterate through the list and print
    * the contents
    private void printData(){

    System.out.println("No of Employees '" + myEmpls.size() + "'.");

    Iterator it = myEmpls.iterator();
    while(it.hasNext()) {

    //Event Handlers
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    tempVal = "";
    if(qName.equalsIgnoreCase("Employee")) {
    //create a new instance of employee
    tempEmp = new Employee();

    public void characters(char[] ch, int start, int length) throws SAXException {
    tempVal = new String(ch,start,length);

    public void endElement(String uri, String localName, String qName) throws SAXException {

    if(qName.equalsIgnoreCase("Employee")) {
    //add it to the list

    }else if (qName.equalsIgnoreCase("Name")) {
    }else if (qName.equalsIgnoreCase("Id")) {
    }else if (qName.equalsIgnoreCase("Age")) {


    public static void main(String[] args){
    SaxParserExample spe = new SaxParserExample();


Similar Threads

  1. Search TextFile
    By in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 12:03 PM
  2. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 08:13 PM
  3. Search
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 11-26-2007, 04:45 AM
  4. Web Search
    By Marcus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-02-2007, 07:51 AM
  5. Search in hibernate
    By Nick15 in forum JDBC
    Replies: 4
    Last Post: 05-09-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts