Total members 11791 |It is currently Sat Aug 17, 2019 1:22 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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");
        }
    }
}

 





Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 getting file name of html input file tag using jsp     -  
 file descriptor vs file pointer     -  
 How to convert xml file to Pdf file using C     -  
 C++ File I/O     -  



Topic Tags

Java XML






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com