How to read XML file in Java – (SAX Parser)
Published: December 7, 2008 , Updated: August 4, 2011 , Author: mkyong
SAX parser is work differently with DOM parser, it either load any XML document into memory nor create any object representation of the XML document. Instead, the SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure.
SAX Parser is faster and uses less memory than DOM parser.See following SAX callback methods :
- startDocument() and endDocument() – Method called at the start and end of an XML document.
- startElement() and endElement() – Method called at the start and end of a document element.
- characters() – Method called with the text contents in between the start and end tags of an XML document element.
1. XML file
Create a simple XML file.
<?xml version="1.0"?> <company> <staff> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> <staff> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <salary>200000</salary> </staff> </company>
2. Java file
Use SAX parser to parse the XML file.
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 ReadXMLFile { public static void main(String argv[]) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean bfname = false; boolean blname = false; boolean bnname = false; boolean bsalary = false; public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException { System.out.println("Start Element :" + qName); if (qName.equalsIgnoreCase("FIRSTNAME")) { bfname = true; } if (qName.equalsIgnoreCase("LASTNAME")) { blname = true; } if (qName.equalsIgnoreCase("NICKNAME")) { bnname = true; } if (qName.equalsIgnoreCase("SALARY")) { bsalary = true; } } public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("End Element :" + qName); } public void characters(char ch[], int start, int length) throws SAXException { if (bfname) { System.out.println("First Name : " + new String(ch, start, length)); bfname = false; } if (blname) { System.out.println("Last Name : " + new String(ch, start, length)); blname = false; } if (bnname) { System.out.println("Nick Name : " + new String(ch, start, length)); bnname = false; } if (bsalary) { System.out.println("Salary : " + new String(ch, start, length)); bsalary = false; } } }; saxParser.parse("c:\\file.xml", handler); } catch (Exception e) { e.printStackTrace(); } } }
Result
Start Element :company Start Element :staff Start Element :firstname First Name : yong End Element :firstname Start Element :lastname Last Name : mook kim End Element :lastname Start Element :nickname Nick Name : mkyong End Element :nickname Start Element :salary Salary : 100000 End Element :salary End Element :staff Start Element :staff Start Element :firstname First Name : low End Element :firstname Start Element :lastname Last Name : yin fong End Element :lastname Start Element :nickname Nick Name : fong fong End Element :nickname Start Element :salary Salary : 200000 End Element :salary End Element :staff End Element :company
Warning
This example may encounter exceptions for UTF-8 XML file, please read this article about how to read the XML “UTF-8″ file in SAX
This example may encounter exceptions for UTF-8 XML file, please read this article about how to read the XML “UTF-8″ file in SAX
Note
You may interest to read this How to read XML file in Java – (DOM Parser)
You may interest to read this How to read XML file in Java – (DOM Parser)







It is help full and worked in my system but simple change i removed public in class definition.
thank you sir …..
what is the meaning of 4 parameters in startElement()
public void startElement(String uri,String localname,String qname,Attributes attr)
please help me with the below error.the file named “file.xml” already exists but still it gives the error.
java.io.FileNotFoundException: d:\file.xml (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:138)
at java.io.FileInputStream.(FileInputStream.java:97)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:629)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:799)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:274)
at ReadXMLFile.main(ReadXMLFile.java:82)
Dear Mkyong,
I found this website and the example very, very useful.
Thank you very much indeed!
best regards,
saraswati.
Your example is wrong. You must EXTEND the DefaultHandler class instead of giving parse() a separate instance. How would SAX know who to call back to otherwise?
To anyone still stuck with the example code: Extend DefaultHandler and call parse() as follows:
My code compiles but I get no output.
I have used both mkyong’s and Sriram’s code but could not get an output.
I placed flags within the code to check if certain section of code are reaches and it seems like it doesn’t enter any of the handler methods.
[...] How to read XML file in Java – (SAX Parser) http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/ Geri ?zleme | Etiketler: callback, Java, parser, RSS, rss parser, [...]
Hello mkyong,
This is a very nice example to start off on knowing how to implement the SAX parser..thank you! ..there is a flaw in your example which may mislead people who are beginning to learn.. the flaw is that .. the boolean flags and the sysouts in the characters(..) method should ideally be in the endElement(..) method..this is because of the nature of SAX parser to handle large data in the text node…where the parser will recursively call character(..) method until the end tag is reached.. to verify just give a very large first name (about 10000+ chars) ..and you will see that only partial data is output… now change the code to as follows and try the same example… you will see that the complete data is displayed as in the source no matter how large the data in the text node is…
public static void main(String[] args) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean bfname = false;
boolean blname = false;
boolean bnname = false;
boolean bsalary = false;
StringBuilder data = null;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
System.out.println(“Start Element :” + qName);
if (qName.equalsIgnoreCase(“FIRSTNAME”)) {
bfname = true;
}
if (qName.equalsIgnoreCase(“LASTNAME”)) {
blname = true;
}
if (qName.equalsIgnoreCase(“NICKNAME”)) {
bnname = true;
}
if (qName.equalsIgnoreCase(“SALARY”)) {
bsalary = true;
}
data = new StringBuilder();
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.equalsIgnoreCase(“FIRSTNAME”)) {
System.out.println(“First Name : ”
+ data.toString());
bfname = false;
}
if (qName.equalsIgnoreCase(“LASTNAME”)) {
System.out.println(“Last Name : ”
+ data.toString());
blname = false;
}
if (qName.equalsIgnoreCase(“NICKNAME”)) {
System.out.println(“Nick Name : ”
+ data.toString());
bnname = false;
}
if (qName.equalsIgnoreCase(“SALARY”)) {
System.out.println(“Salary : ”
+ data.toString());
bsalary = false;
}
System.out.println(“End Element :” + qName);
}
public void characters(char ch[], int start, int length)
throws SAXException {
data.append(new String(ch,start,length));
}
};
InputStream is = SAXParserDemo.class
.getResourceAsStream(“SAXParserInput.xml”);
saxParser.parse(is, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
cheers,
Sriram
[...] specifically, I’ll need an xml parser. Like this:LikeBe the first to like this [...]
startElement and charachters methods,
I am wondering for these methods, is there any specific reason for using separate if-conditions than if-elseif ladder?
I used the if-elseif ladder and seems ok.
Thank you.
hi
i am trying to find difference between two xml files please help me on this….
[...] A partir de esto podríamos, en lugar de escribir por pantalla los valores, procesarlos y crear atributos de objetos de clases que hayamos definido nosotros. Un ejemplo de esto se puede encontrar aquí. [...]
super example
Very straightforward, simple example. Nice job.
how to search xml file data based on the given keyword from html form using sax parser?
Hi
I work in Middle office and need to parse and write thousands of fixml messages. Can you please let me know the fastet xml parser some thing like that (10000/min) and share a tutorial
Thanks
Sudar
I want to parse a content some thing like this. It is html formatted content.
I want the values present in tags like name ,age,sex etc. How can I do that?
Service: Employee File 10 records
Company: A2B company
Name
Employee number
Sex
Age
Designation
Date
Mark
1001
Male
25
Analyst
2005-02-01
ricky
1005
Male
28
Analyst
2008-12-01
David
1007
Male
35
SeniorAnalyst
2005-08-11
hilary
1008
female
28
maketing
2001-02-01
Hi,
Can anyone please tell me how to Parse multiple XML files and store the result in a single TEXT file.
Just read xml file by file normal and store the result in a single text file. What’s your problem?
can u explain with an example what is the difference between SAX Parser and DOM Parser?
DOM – reads all structure into memory, and data stays in memory, and next you can read your data from memory, and make a lot of operations such as search. (useful for small files)
SAX – nothing is stored in memory, so you can’t restore any data by later operations. file is parsed once, and you must catch data while it is parsed (useful for large files)
Thanks for your invaluable inputs. Some extras,
DOM – Random access to the XML file.
SAX – Using callback mechanism, parse XML file from top to bottom hierarchy, ya usually in large file which required access sequentially.
Hi mkyong,
I am using sax parser to read the xml file. Parser is reading only some part of my xml file. when I print Char[] of characters method it’s showing some part of my xml. Please help me on this Below my files..
Thanks in advance..
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean dbFlag = false;
boolean flag = false;
public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException {
if (queryname != null && !queryname.isEmpty()) {
if (qName.equalsIgnoreCase(“FIW”)) {
for (int i = 0; i < attributes.getLength(); i++) {
if (attributes.getValue(i).equals(queryname))
flag = true;
}
}
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
if (queryname != null && !queryname.isEmpty()) {
if (flag) {
System.out.println(ch);
connDTO.setQuery(new String(ch, start, length));
flag = false;
}
}
}
};
File file = new File(xmlurl);
InputStream inputStream= new FileInputStream(file);
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
saxParser.parse(is, handler);
xml file—
SELECT SISO.CEI_T1_NAM as DistributorName, SISO.CEI_T1 as DistributorCEID, SISO.CEI_T2_NAM as T2ResellerName, SISO.CEI_T2 as T2ReSellerCEID, SISO.CUST_NAM_EU as EndUserName, SISO.CMR_CUSTNO_EU as EndUserCEID, SISO.BRAND_DESC as Brand, SISO.SPECIAL_BID_FLG as Runrate, SISO.PROD_ID as ProdId, REF.TYPE as MachType, REF.MODL as MachMdl, SISO.INVOC_AMT_LOC as RevenueLocal, SISO.LOC_CRNCY_CD as Currency, SISO.SO_QTY as Quantity, SISO.SER_NO as SerialNum, SISO.ORD_NO as OrderNo, SISO.INVOC_NO as InvoiceNo, SISO.SALE_OUT_DT as SellOutDate, SISO.IW_TS as UpdTmsp FROM WSDIW.BPAP_IO_SISO SISO ,WSDIW.IM_TPRSS_REF REF WHERE SISO.PROD_ID=REF.PID_12 AND SISO.IMT_T1 IN(‘ISA’,'KOR’) AND SISO.BRAND_DESC in(‘xSeries’,'zSeries’,'Storage’,'RSS’,'pSeries’) AND SISO.CHANL_CD IN (‘F’,'G’,'H’,'J’,'K’,'I’,'M’,'Q’,'Y’) AND SISO.PROD_DIV IN (’13′,’26′,’2D’,’2K’,’2W’,’4S’,’71′,’9R’,’75′,’Z3′) AND SISO.IW_TS > ? AND YEAR(SISO.IW_TS) = (SELECT YEAR(CURRENT DATE) FROM SYSIBM.SYSDUMMY1) WITH UR;
select count(*) from WSDIW.BPAP_IO_SISO a,WSDIW.IM_TPRSS_REF b where a.PROD_ID=b.PID_12 AND a.IMT_T1 IN(?,?) AND a.BRAND_DESC in(‘xSeries’,'zSeries’,'Storage’,'RSS’,'pSeries’) AND year(a.sale_out_dt)=(select year(current date) from sysibm.sysdummy1)
select count(*) from FIRCC.COUNTRY_R where CURRENCY_TYPE !=” and CURRENCY_TYPE is not null
INSERT INTO [BPDMMAIN].[DBO].[BPDM_TEMP] (DistributorName, DistributorCEID, T2ResellerName, T2ReSellerCEID, EndUserName, EndUserCEID, Brand, Runrate, MachType, MachMd, ProdId, RevenueLocal, Currency, Quantity, SerialNum, OrderNo, InvoiceNo, SellOutDate, UpdTmsp) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
SELECT FULLDETAIL.LCTRYNUM AS Lctrynum, FULLDETAIL.RCTRYNUM AS Rctrynum, (SELECT CUST.CUSTNAM FROM FIRCC.CUSTOMER_ACCOUNT_R CUST WHERE CUST.CUSTNUM = FULLDETAIL.CUSTNUM AND CUST.LCTRYNUM = FULLDETAIL.LCTRYNUM) AS CustomerName,FULLDETAIL.CUSTNUM AS CustomerNum, PRODUCT.TPRSS_BMDIV AS BrandCode,FULLDETAIL.PRODID AS ProdId, FULLDETAIL.SERIALNUM AS SerialNo, FULLDETAIL.ACCTMO AS TransactionMonth, FULLDETAIL.ENTRY_DATE8 AS TransactionDate, FULLDETAIL.AMTLOC_REP RevenueLocal, (SELECT COUNTRY.CURRENCY_TYPE FROM FIRCC.COUNTRY_R COUNTRY WHERE COUNTRY.CTRYNUM = FULLDETAIL.LCTRYNUM and COUNTRY.REGCODE in (‘ISA’,'KOR’)) AS Currency,FULLDETAIL.INV_NBR AS InvoiceNo, FULLDETAIL.QUANTITY AS Quantity,FULLDETAIL.BILL_TO_CUSTOMER AS BillToCustomer,FULLDETAIL.ORDERNUM as OrderNum,FULLDETAIL.LAST_UPT_TIME AS UpdTmsp FROM FIRCC.FULLDETAIL_CM_D FULLDETAIL,FIRCC.PRODUCT_R PRODUCT WHERE PRODUCT.PRODID = FULLDETAIL.PRODID AND PRODUCT.TPRSS_BMDIV IN (’13′,’26′,’2D’,’2K’,’2W’,’4S’,’71′,’9R’,’75′,’Z3′) AND FULLDETAIL.CHANID IN (‘F’,'G’,'H’,'J’,'K’,'I’,'M’,'Q’,'Y’) AND FULLDETAIL.MAJOR IN (’300′,’302′,’304′,’306′,’309′,’312′,’330′,’332′) AND FULLDETAIL.MINOR IN (’0100′,’0400′,’0401′,’0403′,’0600′,’0700′,’0900′) AND FULLDETAIL.ACCID IN (‘CSL’,'CIS’,'SPL’,'CRF’,'SLF’) AND FULLDETAIL.LAST_UPT_TIME > ? AND YEAR(FULLDETAIL.LAST_UPT_TIME) = (SELECT YEAR(CURRENT DATE) FROM SYSIBM.SYSDUMMY1) WITH UR;
I tried to read xml file ( approx. 600 MB ) on 3.2GB RAM computer and i got outofmemory exception with XOM, VTD-XML ex. Only this code makes it successfully. Thank you
yes, SAX is designed to parse large XML file. Thanks for sharing your experience.
[...] ?SAX??xml?? [...]
Perfect Hello World to see how to parse a file using SAX. Thank you!
On executing the code, I get “javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.SAXParserFactoryImpl not found”.
I’ve already added following JARs to classpath:
xercesImpl-2_9_1 , xml-apis-2_9_1
Any help is greatly appreciated !!
I think that this is the best tutorial about SAX and XML!
:)
[...] Read XML file – (SAX Parser) Example to read a XML file with SAX parser. [...]
[...] You can compare the syntax with SAX and DOM parser in the following two examples – How to read XML file in Java – (SAX Parser) – How to read XML file in Java – (DOM [...]
[...] Written on December 17, 2009 at 6:04 am by mkyong Here’s my previous article about how to read XML file in Java SAX Parser. The previous example is working fine to parse the plain text (ANSI) XML file, however, it may [...]
[...] This is the Java’s example i used to read the XML file with SAX. [...]
while i try to compile the sax parser code i got the following error:
org.xml.sax.SAXParseException: Content is not allowed in prolog.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at com.mypack.ReadXMLFileSAX.main(ReadXMLFileSAX.java:99)
This may cause by the invalid XML content. please read
http://www.mkyong.com/java/sax-error-content-is-not-allowed-in-prolog/
Or, can you provide me your XML file?
[...] If you want to compare different implementation between DOM and SAX, Please visit here for How to read XML file in Java – (SAX Parser) [...]