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

    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
    Posts
    7
    Rep Power
    0

    Default SaxParserExample

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

    import java.io.IOException;
    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() {
    parseDocument();
    printData();
    }

    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) {
    se.printStackTrace();
    }catch(ParserConfigurationException pce) {
    pce.printStackTrace();
    }catch (IOException ie) {
    ie.printStackTrace();
    }
    }

    /**
    * 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()) {
    System.out.println(it.next().toString());
    }
    }


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


    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
    myEmpls.add(tempEmp);

    }else if (qName.equalsIgnoreCase("Name")) {
    tempEmp.setName(tempVal);
    }else if (qName.equalsIgnoreCase("Id")) {
    tempEmp.setId(Integer.parseInt(tempVal));
    }else if (qName.equalsIgnoreCase("Age")) {
    tempEmp.setAge(Integer.parseInt(tempVal));
    }

    }

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

    }

Similar Threads

  1. Search TextFile
    By gsupriyarao@yahoo.com in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 11:03 AM
  2. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 07:13 PM
  3. Search
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 11-26-2007, 03:45 AM
  4. Web Search
    By Marcus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-02-2007, 06:51 AM
  5. Search in hibernate
    By Nick15 in forum JDBC
    Replies: 4
    Last Post: 05-09-2007, 01: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
  •