Switch to full style
General Java code examples
Post a reply

parse XML file using SAX

Fri Oct 26, 2012 2:24 pm

parse XML file using SAX, the xml file is (Employees.xml):

Code:
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
    <Employee EmploymentDate="12-11-2001">
       <Name>joseph</Name>
       <DeptID>21</DeptID>
       <Title>Senior</Title>
    </Employee>
    <Employee EmploymentDate="3-2-2004">
       <Name>Tom</Name>
       <DeptID>23</DeptID>
       <Title>junior</Title>
    </Employee>
</Employees>


The java SAX parser :
Code:


import java
.util.jar.Attributes;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

 
public class SAX_Parse extends DefaultHandler 
{

    private String value;

    public static void main(String[] args) {
        // Printing the headers of the XML file
        System.out.println("EmploymentDate\tName\tDeptID\tTitle\n");
        // 
        new SAX_Parse().XMLStartParse();
    }

    public void XMLStartParse() {
        try {
            // getting SAXParserFactory instance
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

            // Getting SAXParser object from AXParserFactory instance
            SAXParser saxParser = saxParserFactory.newSAXParser();

            // Parsing XML Document by calling parse method of SAXParser class
            saxParser.parse("C:\\employees.xml", this);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Overriding characters() method of DefaultHandler class
    @Override
    public void characters
(char[] ch, int start, int length)
            throws SAXException {
        value = new String(ch, start, length);
    }


    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        value = "";
        if (qName.equalsIgnoreCase("Employee")) {
            System.out.print(attributes.getValue("EmploymentDate") + "\t");
        }
    }


    @Override
    public void endElement
(String uri, String localName, String qName)
            throws SAXException {
        if (qName.equalsIgnoreCase("Name")) {
            System.out.print(value + "\t");
        }

        if (qName.equalsIgnoreCase("EmpID")) {
            System.out.print(value + "\t");
        }
        if (qName.equalsIgnoreCase("Title")) {
            System.out.print(value + "\n");
        }
    }
}

 




Post a reply
  Related Posts  to : parse XML file using SAX
 Parse XML using DOM file and run XPath query     -  
 Parse URL in java get Protocol,File,Reference,Host and Port     -  
 Web scraping and Parse using java... plz help i really need.     -  
 Load RSS feeds and parse it with JQuery     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 Copy file to file in java code- implementation     -  
 file descriptor vs file pointer     -  
 getting file name of html input file tag using jsp     -  
 How to convert xml file to Pdf file using C     -  
 C++ File I/O     -  

Topic Tags

Java XML