Tutorial

Java SAX Parser Example

Published on August 4, 2022
author

Pankaj

Java SAX Parser Example

SAX Parser in java provides API to parse XML documents. SAX parser is different from DOM parser because it doesn’t load complete XML into memory and read xml document sequentially.

SAX Parser

sax parser, sax parser example, java sax parser javax.xml.parsers.SAXParser provides method to parse XML document using event handlers. This class implements XMLReader interface and provides overloaded versions of parse() methods to read XML document from File, InputStream, SAX InputSource and String URI. The actual parsing is done by the Handler class. We need to create our own handler class to parse the XML document. We need to implement org.xml.sax.ContentHandler interface to create our own handler classes. This interface contains callback methods that receive notification when an event occurs. For example StartDocument, EndDocument, StartElement, EndElement, CharacterData etc. org.xml.sax.helpers.DefaultHandler provides default implementation of ContentHandler interface and we can extend this class to create our own handler. It’s advisable to extend this class because we might need only a few of the methods to implement. Extending this class will keep our code cleaner and maintainable.

SAX parser Example

Let’s jump to the SAX parser example program now, I will explain different features in detail later on. employees.xml

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
	<Employee id="1">
		<age>29</age>
		<name>Pankaj</name>
		<gender>Male</gender>
		<role>Java Developer</role>
	</Employee>
	<Employee id="2">
		<age>35</age>
		<name>Lisa</name>
		<gender>Female</gender>
		<role>CEO</role>
	</Employee>
	<Employee id="3">
		<age>40</age>
		<name>Tom</name>
		<gender>Male</gender>
		<role>Manager</role>
	</Employee>
	<Employee id="4">
		<age>25</age>
		<name>Meghna</name>
		<gender>Female</gender>
		<role>Manager</role>
	</Employee>
</Employees>

So we have a XML file stored somewhere in file system and by looking at it, we can conclude that it contains list of Employee. Every Employee has id attribute and fields age, name, gender and role. We will use SAX parser to parse this XML and create a list of Employee object. Here is the Employee object representing Employee element from XML.

package com.journaldev.xml;

public class Employee {
    private int id;
    private String name;
    private String gender;
    private int age;
    private String role;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    
    @Override
    public String toString() {
        return "Employee:: ID="+this.id+" Name=" + this.name + " Age=" + this.age + " Gender=" + this.gender +
                " Role=" + this.role;
    }
    
}

Let’s create our own SAX Parser Handler class extending DefaultHandler class.


package com.journaldev.xml.sax;

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

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

import com.journaldev.xml.Employee;

public class MyHandler extends DefaultHandler {

	// List to hold Employees object
	private List<Employee> empList = null;
	private Employee emp = null;
	private StringBuilder data = null;

	// getter method for employee list
	public List<Employee> getEmpList() {
		return empList;
	}

	boolean bAge = false;
	boolean bName = false;
	boolean bGender = false;
	boolean bRole = false;

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

		if (qName.equalsIgnoreCase("Employee")) {
			// create a new Employee and put it in Map
			String id = attributes.getValue("id");
			// initialize Employee object and set id attribute
			emp = new Employee();
			emp.setId(Integer.parseInt(id));
			// initialize list
			if (empList == null)
				empList = new ArrayList<>();
		} else if (qName.equalsIgnoreCase("name")) {
			// set boolean values for fields, will be used in setting Employee variables
			bName = true;
		} else if (qName.equalsIgnoreCase("age")) {
			bAge = true;
		} else if (qName.equalsIgnoreCase("gender")) {
			bGender = true;
		} else if (qName.equalsIgnoreCase("role")) {
			bRole = true;
		}
		// create the data container
		data = new StringBuilder();
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (bAge) {
			// age element, set Employee age
			emp.setAge(Integer.parseInt(data.toString()));
			bAge = false;
		} else if (bName) {
			emp.setName(data.toString());
			bName = false;
		} else if (bRole) {
			emp.setRole(data.toString());
			bRole = false;
		} else if (bGender) {
			emp.setGender(data.toString());
			bGender = false;
		}
		
		if (qName.equalsIgnoreCase("Employee")) {
			// add Employee object to list
			empList.add(emp);
		}
	}

	@Override
	public void characters(char ch[], int start, int length) throws SAXException {
		data.append(new String(ch, start, length));
	}
}

MyHandler contains the list of the Employee object as a field with a getter method only. The Employee objects are getting added in the event handler methods. Also, we have an Employee field that will be used to create an Employee object and once all the fields are set, add it to the employee list.

SAX parser methods to override

The important methods to override are startElement(), endElement() and characters(). SAXParser starts parsing the document, when any start element is found, startElement() method is called. We are overriding this method to set boolean variables that will be used to identify the element. We are also using this method to create a new Employee object every time Employee start element is found. Check how id attribute is read here to set the Employee Object id field. characters() method is called when character data is found by SAXParser inside an element. Note that SAX parser may divide the data into multiple chunks and call characters() method multiple times (Read ContentHandler class characters() method documentation). That’s why we are using StringBuilder to keep this data using append() method. The endElement() is the place where we use the StringBuilder data to set employee object properties and add Employee object to the list whenever we found Employee end element tag. Below is the test program that uses MyHandler to parse above XML to list of Employee objects.

package com.journaldev.xml.sax;

import java.io.File;
import java.io.IOException;
import java.util.List;

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

import org.xml.sax.SAXException;

import com.journaldev.xml.Employee;

public class XMLParserSAX {

    public static void main(String[] args) {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    try {
        SAXParser saxParser = saxParserFactory.newSAXParser();
        MyHandler handler = new MyHandler();
        saxParser.parse(new File("/Users/pankaj/employees.xml"), handler);
        //Get Employees list
        List<Employee> empList = handler.getEmpList();
        //print employee information
        for(Employee emp : empList)
            System.out.println(emp);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
    }
    }

}

Here is the output of the above program.

Employee:: ID=1 Name=Pankaj Age=29 Gender=Male Role=Java Developer
Employee:: ID=2 Name=Lisa Age=35 Gender=Female Role=CEO
Employee:: ID=3 Name=Tom Age=40 Gender=Male Role=Manager
Employee:: ID=4 Name=Meghna Age=25 Gender=Female Role=Manager

SAXParserFactory provides factory methods to get the SAXParser instance. We are passing File object to the parse method along with MyHandler instance to handle the callback events. SAXParser is a little bit confusing in the start but if you are working on a large XML document, it provides a more efficient way to read XML than DOM Parser. That’s all for SAX Parser in Java.

You can download the project from our GitHub Repository.

Reference: SAXParser, DefaultHandler

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial
Tags:

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 30, 2013

Hi Pankaj, i appreciate your contributions. pls i am writing a GUI calculator program. When i click on a number, it displays in the textfield, but, the moment i click on another, it overrides the previous. how do i go about this?

- Jide

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 30, 2013

I am sorry but I haven’t worked on Swings.

- Pankaj

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 21, 2014

    If the area where you display the number is a Text Area, then you should try using appedText instead of setText.

    - Manooj

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      September 19, 2013

      Just append the label of button with textbox content and set It again. String num=t1.getText()+btn.getLabel(); t1.setText(num); something like this

      - Varsha

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        November 1, 2013

        Thank you very much, very clear explanations. I’ve been trying for several hours to wrap my head around the concept and usage of the SAX API and you made it easy for me. Thank you again!

        - Andres

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          November 9, 2013

          Hi Pankaj, Is the response data Should be in request.Is there any chance for hidden… Means already stored data we need with some reference field. like through Employee Id, we need to get Employee details(already Employee Details are stored)… How it is???

          - pallavi

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          November 11, 2013

          You might parse Emp ID from the XML and based on that get full details from database or from other systems as per your requirement.

          - Pankaj

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            November 22, 2013

            Hi, For example using startelement I’m passing as but in the endelement I should get only as not as .I have used ContentHandler for this. How can we implement to have different name in header and footer. Any help can be greatly appreciated!

            - Ariyur

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              February 18, 2014

              HI, SIR… its fine for single xml document, if in case we are using more than one xml document how it will differentiate and store. ex: you are taken employee xml document, i am inserting student.xml doc and book.xml document in that how it will identify individually and store. if we enter pankaj how it will identify whether it is student name or employee name or author name.

              - ashwini

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 18, 2014

                Hi, when running the program I get the following output: pojos.Employee@1a3ca10 pojos.Employee@26f9e5 pojos.Employee@e06703 pojos.Employee@8b1a4f rename packages what is the problem?

                - Emmanuel

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 18, 2014

                I think you missed the toString() implementation in the Employee class, that’s why it’s printing above, using Object class implementation of toString() method.

                - Pankaj

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  June 3, 2014

                  How to parse empty tags? I mean example is cool but very simple. I can’t figure out how to parse short notation tags: or empty ones but lik …this is correct xml, but according to code above, next node will be treated as content of it…

                  - Ellen

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    June 12, 2014

                    Hi Pankaj, I have no coding experience and I’m trying to parse an xml file. I used your code and modified it. When I run it, i get this: Exception in thread “main” java.lang.NullPointerException at edu.illinois.lis.hakimra2.myproject.MyHandler.characters(MyHandler.java:72) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.characters(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source) at javax.xml.parsers.SAXParser.parse(Unknown Source) at edu.illinois.lis.hakimra2.myproject.XMLParserSAX.main(XMLParserSAX.java:19) Do you know what might the problem be? Also, I want to store my data in a csv file. Can you tell me how to do it? Thanks

                    - Dalal

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    June 15, 2014

                    Check the code, NullPointerException is very easy to solve, figure out which object is null in that line.

                    - Pankaj

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      July 9, 2014

                      Can you help me, how to do sax parsing for be low xml 1 PROD=DPP;PLC=IOYR2013NSSN 1003N.3::DPP::0 94 94 15 15 94 11.6 12.34 15.95 10.5 CF=1;PLC=IOYR2013NSSN;M_ITEMSELL:MFY.=93.85;M_SALESTAX:TFN.=7.37;M_SHIPPING:SFYS=7.78;M_SHIPSUR:SFYS=0;P_ITEMSELL:MFN.=94;P_SHIPSUR:SFNS=0;P_TSH:SFNS=15;A_ITEM:PAX=94;A_ITEM:USD=94;A_SHIP:PAX=15;A_SHIP:USD=15;D_PV:PAX=15.95;D_BV:PAX=10.5 94 94 15 15 94 11.6 12.34 15.95 10.5 CF=1;PLC=IOYR2013NSSN;M_ITEMSELL:MFY.=93.85;M_SALESTAX:TFN.=7.37;M_SHIPPING:SFYS=7.78;M_SHIPSUR:SFYS=0;P_ITEMSELL:MFN.=94;P_SHIPSUR:SFNS=0;P_TSH:SFNS=15;A_ITEM:PAX=94;A_ITEM:USD=94;A_SHIP:PAX=15;A_SHIP:USD=15;D_PV:PAX=15.95;D_BV:PAX=10.5

                      - ramesh

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      July 9, 2014

                      You need to post after escaping < and > signs.

                      - Pankaj

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      July 10, 2014

                      1 PROD=DPP;PLC=IOYR2013NSSN 1003N.3::DPP::0 94 94 15 15 94 11.6 12.34 15.95 10.5 CF=1;PLC=IOYR2013NSSN;M_ITEMSELL:MFY.=93.85;M_SALESTAX:TFN.=7.37;M_SHIPPING:SFYS=7.78;M_SHIPSUR:SFYS=0;P_ITEMSELL:MFN.=94;P_SHIPSUR:SFNS=0;P_TSH:SFNS=15;A_ITEM:PAX=94;A_ITEM:USD=94;A_SHIP:PAX=15;A_SHIP:USD=15;D_PV:PAX=15.95;D_BV:PAX=10.5 94 94 15 15 94 11.6 12.34 15.95 10.5 CF=1;PLC=IOYR2013NSSN;M_ITEMSELL:MFY.=93.85;M_SALESTAX:TFN.=7.37;M_SHIPPING:SFYS=7.78;M_SHIPSUR:SFYS=0;P_ITEMSELL:MFN.=94;P_SHIPSUR:SFNS=0;P_TSH:SFNS=15;A_ITEM:PAX=94;A_ITEM:USD=94;A_SHIP:PAX=15;A_SHIP:USD=15;D_PV:PAX=15.95;D_BV:PAX=10.5

                      - ramesh

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        July 10, 2014

                        ?xml version=“1.0” encoding=“UTF-8”? pricelists pricelist priceListId=“1003N.3::DPP::0” action=“replace” conversionFactor1/conversionFactor priceListSummaryPROD=DPP;PLC=IOYR2013NSSN/priceListSummary ccus ccu action="replace"1003N.3::DPP::0/ccu /ccus skus sku skuId=“52868-BLK” action=“replace” itemUSD94/itemUSD itemPAX94/itemPAX shipUSD15/shipUSD shipPAX15/shipPAX originalPAX94/originalPAX marginAmountUSD11.6/marginAmountUSD marginPercent12.34/marginPercent customValues customValue name="PV"15.95/customValue customValue name="BV"10.5/customValue /customValues maritzSummaryCF=1;PLC=IOYR2013NSSN;M_ITEMSELL:MFY.=93.85;M_SALESTAX:TFN.=7.37;M_SHIPPING:SFYS=7.78;M_SHIPSUR:SFYS=0;P_ITEMSELL:MFN.=94;P_SHIPSUR:SFNS=0;P_TSH:SFNS=15;A_ITEM:PAX=94;A_ITEM:USD=94;A_SHIP:PAX=15;A_SHIP:USD=15;D_PV:PAX=15.95;D_BV:PAX=10.5/maritzSummary /sku sku skuId=“52868-BLU” action=“replace” itemUSD94/itemUSD itemPAX94/itemPAX shipUSD15/shipUSD shipPAX15/shipPAX originalPAX94/originalPAX marginAmountUSD11.6/marginAmountUSD marginPercent12.34/marginPercent customValues customValue name="PV"15.95/customValue customValue name="BV"10.5/customValue /customValues maritzSummaryCF=1;PLC=IOYR2013NSSN;M_ITEMSELL:MFY.=93.85;M_SALESTAX:TFN.=7.37;M_SHIPPING:SFYS=7.78;M_SHIPSUR:SFYS=0;P_ITEMSELL:MFN.=94;P_SHIPSUR:SFNS=0;P_TSH:SFNS=15;A_ITEM:PAX=94;A_ITEM:USD=94;A_SHIP:PAX=15;A_SHIP:USD=15;D_PV:PAX=15.95;D_BV:PAX=10.5/maritzSummary /sku /skus /pricelist /pricelists

                        - ramesh

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          August 7, 2014

                          Nice article. I can take this as reference for using SAX kind of parser and build on it… Thanks!

                          - Nagaraj

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            August 29, 2014

                            When I run the above code, I am getting “java.io.FileNotFoundException: C:\Users\420117\Desktop\employee.xml (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source) at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source) at javax.xml.parsers.SAXParser.parse(Unknown Source) at com.journeldev.xml.sax.XMLParserSAX.main(XMLParserSAX.java:25)” Means “File Not Found Exception”… But , I verified the path very well of my xml file. Still I am getting the same error… Would some body help …

                            - Archu

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            August 29, 2014

                            If file is there, then it seems to be access issue.

                            - Pankaj

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              October 21, 2014

                              sometime we forgot saxparser.parse(“filename”,handle) and miss final type "xml " --> saxparser.parse(“filename.xml”,handle)

                              - phuong

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                November 17, 2014

                                be sure in file name you look for employee.xml and author save it like employees.xml

                                - Dima

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  October 15, 2017

                                  Try putting double back slash as shown below: C:\Users\420117\Desktop\employee.xml

                                  - MsCoder

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    September 24, 2014

                                    I am trying the above code but the attribute is getting null… plz help

                                    - Kunal

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      September 26, 2014

                                      Can you please explain how do i dump xml data into database?

                                      - SJK

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      September 26, 2014

                                      Once you have XML data, you can create a String from it and save it in the DB table VARCHAR type column, for large XML/Text data, you should use CLOB data type.

                                      - Pankaj

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        October 18, 2014

                                        Hello Pankaj Sir, I do not want to read whole xml file at a time. I want to read only 50 or 100 or any fix number of records at a time. And display them to user and when user click on next then read next records.Like Pagination How to do that .

                                        - Atul Goyal

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          November 3, 2014

                                          Hi Pankaj, Thank you for your contribution. It really worked for my requirements.

                                          - Josh

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            January 7, 2015

                                            i have a kml file which has multiple placemark objects. in placemark object there is multiple coordinates tag i want to read all coordinates tag in the placemark object. thanks

                                            - fakhruddin

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              February 5, 2015

                                              hi everyone , any one can solve my prob i am i big prob i am sending my prob if some one can solve then plz send the solution on my mail Id =“tonibrown212@gmail.com” this is urgent ============================= Problem statement ================ Objective is to create a REST web-service (or a simple Java program) which converts a XML from hierarchical format to linear format. Input XML would be hierarchical in nature. There will a element which can contain any number of other elements and so on. The will have an optional element . Weare interested in the element and we want to convert this XML to a linear format where there is a element for each element. The name of that should be the path to that element. Please note that there is no fixed depth of this XML and the depth is non-uniform (ragged-hierarchy). The will be there at the leaf-level nodes but it may/may not appear for parent level nodes. Input XML A B C 3.0 D 4.0 7.0 Output XML A/B/C 3.0 A/B/D 4.0 A/B 7.0

                                              - Toni

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                March 17, 2015

                                                Pankaj, I am able to parse the XML into java objects. But whenever the XML finds and “Empty tag” it pring with blank space for that particular java object. Could you pls let me know how to handle EMpty tags. I want to pring “Null” if there is any empty tag instead of blank space. TIA.

                                                - Ravi

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  March 23, 2015

                                                  Exception in thread “main” java.lang.OutOfMemoryError: Java heap space at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649) at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217) at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:329) at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1256) at uk.ac.ucl.ee.fnss.Parser.parseTrafficMatrixSequence(Unknown Source) at kr.re.kisti.hieuvt.core.HelperTp.getTrafficMatrixAtTime(HelperTp.java:69) at kr.re.kisti.hieuvt.core.HelperTp.updateTrafficMap(HelperTp.java:87) at kr.re.kisti.hieuvt.core.HelperTp.classifyVms(HelperTp.java:115) at kr.re.kisti.hieuvt.run.RandomRunnerTp.init(RandomRunnerTp.java:81) at org.cloudbus.cloudsim.examples.power.RunnerAbstract.(RunnerAbstract.java:95) at kr.re.kisti.hieuvt.run.RunnerAbstractTp.(RunnerAbstractTp.java:52) at kr.re.kisti.hieuvt.run.RandomRunnerTp.(RandomRunnerTp.java:58) at kr.re.kisti.hieuvt.run.LrMmtTp.main(LrMmtTp.java:40)

                                                  - soniya

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    June 23, 2015

                                                    hi pankaj can you tell me how cam i print a specific xml block.like in your example i want to print only 35 Lisa Female CEO

                                                    - amit

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    June 23, 2015

                                                    with xml tag

                                                    - amit

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      June 24, 2015

                                                      I have problem in XML Parsing… anyone help me…

                                                      - Saqib Rana

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        March 10, 2016

                                                        - - - i want to extract data between plz can u help…???

                                                        - Bhadri

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          July 15, 2016

                                                          Thanks for the post …it is useful for me…

                                                          - sandip gavade

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            July 20, 2016

                                                            I don’t understand why you created a boolean for each of the elements of your employee. Is this something one would need to do for every element or just the ones I care about? I have a much more complex xml file that contains many subfolders

                                                            - Steve

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              August 21, 2016

                                                              Hi,thanks for your usefoul example. I have a clarification on how I can modify the Employee.java object representing Employee element from XML. For example, if the employees.xml has additional attributes, how can I rapresent them in the Employee class in order to generate getter and setter methods to store the values of these attributes ? For example in your employees.xml file I added on employee 2 , the attribute Type=“Crypted3des” on tag name and on tg .gender. 35 Lisa Female CEO Thanks in advance. Alex

                                                              - Alex

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                September 24, 2016

                                                                Hi , Issue --> Actually I am facing issue with xml parsing (SAX Parser) in Unix Machine. Same Jar/Java-Code behave differently on windows and Unix Machine, why ? :( Windows Machine --> works fine , Using SAX Parser to load huge xml file , Read all values correctly and populate same values. Charset.defaultCharset() windows-1252 Unix Machine --> After then created JAR and deployed at Unix --> tomcat and execute the jar. Tried to load same huge xml file But noticed that some values or characters are populated empty or incomplete like Country Name populated as “ysia” instead of “Malaysia” or transaction Date populate as “3 PM” instead of “18/09/2016 03:31:23 PM”. Charset.defaultCharset() UTF-8 Issue is only with Unix , Because when I load same xml at windows or my local eclipse it works fine and all values populate correctly. Also I tried to modify my code and set encoding as UTF-8 for inputSteamReader but still it’s not read value correctly at unix box. Note : There is no special characters in xml. Also noticed one thing that when I take out same records (those value not populated correctly) in other xml file and load in unix machine with same jar it works fine. It means issues occur while load these records with huge data. :( Please suggest , What should be the solution ?

                                                                - Amit

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                November 5, 2018

                                                                A bit late but might help others: Be aware that the example given here is WRONG!!! SAX-Parser CAN and WILL split fields arbitrarily for calling the ‘characters’ method. Assign the result to anything will lead to problems. This most often manifest in incomplete strings - a field containing the name of a street might have “Very long overly complicated streetname” in it, but SAX splits it up into “Very long overly compli” and “cated streetname” - which leads to only “cated streetname” being assigned to your variable. The correct way to handle this is using a buffer (String-builder for example) in your handler. In “startElement” empty the buffer, in “characters” fill the buffer (append) and in “endElement” handle the full content of the buffer.

                                                                - Alexa

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                November 22, 2018

                                                                Hi Alexa, Thanks for pointing out the issue in the code. Yes, you are correct that characters() method can be called multiple times and we should use a buffer to hold this data. I never encountered this issue in my project work, I have corrected the code.

                                                                - Pankaj

                                                                  JournalDev
                                                                  DigitalOcean Employee
                                                                  DigitalOcean Employee badge
                                                                  October 27, 2016

                                                                  Your example was amazing… Its very easy to understand and I fixed my issue using this example.

                                                                  - Aishwarya

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    November 24, 2016

                                                                    when to use SAX parser and when to use DOM parser?

                                                                    - Sundara Baskaran

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    July 13, 2017

                                                                    When you have large XML file go with SAX parser otherwise go with DOM parser

                                                                    - Vignesh.P

                                                                      JournalDev
                                                                      DigitalOcean Employee
                                                                      DigitalOcean Employee badge
                                                                      June 2, 2017

                                                                      Hi, Could you pls give example for unstructured xml file?

                                                                      - Tint

                                                                        JournalDev
                                                                        DigitalOcean Employee
                                                                        DigitalOcean Employee badge
                                                                        July 10, 2017

                                                                        Is that possible if my xml have 100 Employee tag and I want to parse 20 employee at a time. I don’t want to get the a list of 100 employee in my memory.So, I just want to parse 20 employee in a single list.

                                                                        - Rahul

                                                                        JournalDev
                                                                        DigitalOcean Employee
                                                                        DigitalOcean Employee badge
                                                                        July 13, 2017

                                                                        Ya it is possible…i think you can use SAX parse…correct me if i am wrong

                                                                        - Vignesh.P

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          August 23, 2018

                                                                          i want to display only specific tags of xml file in jtree i am using sax parser and defaultmutabletreenode here is my code: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.tree.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import org.apache.xerces.parsers.*; public class ScfA { // private String currentElement; private SAXTreeBuilder saxTree = null; private static String file = “”; public static void main(String args[]) { JFrame frame = new JFrame(“[ PROSscf ]”); frame.setSize(400, 400); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); } }); file = “lastscf.xml”; new ScfA(frame); } public ScfA(JFrame frame) { frame.getContentPane().setLayout(new BorderLayout()); DefaultMutableTreeNode top = new DefaultMutableTreeNode(file); // DefaultMutableTreeNode top1= new // DefaultMutableTreeNode(“XML Document”); saxTree = new SAXTreeBuilder(top); try { SAXParser saxParser = new SAXParser(); saxParser.setContentHandler(saxTree); saxParser.parse(file); } catch (Exception ex) { top.add(new DefaultMutableTreeNode(ex.getMessage())); } JTree tree = new JTree(saxTree.getTree()); JScrollPane scrollPane = new JScrollPane(tree); frame.getContentPane().add(“Center”, scrollPane); frame.setVisible(true); } } class SAXTreeBuilder extends DefaultHandler { private DefaultMutableTreeNode currentNode = null; private DefaultMutableTreeNode previousNode = null; private DefaultMutableTreeNode rootNode = null; public SAXTreeBuilder(DefaultMutableTreeNode root) { rootNode = root; currentNode = rootNode; } public DefaultMutableTreeNode getRoot() { return rootNode; } // check whether required or not public void characters(char[] data, int start, int end) { String str = new String(data, start, end); if (!str.equals(“”) && Character.isLetter(str.charAt(0))) { currentNode.add(new DefaultMutableTreeNode(str)); } } public void startElement(String uri, String qName, String lName, Attributes atts) throws SAXException { /* if (“Application”.equals(currentNode)) */ /* String name = (String)currentNode.getUserObject(); */ previousNode = currentNode; // Use the result of the getNodeText method to construct the tree currentNode = new DefaultMutableTreeNode(getNodeText(lName, atts)); // Add attributes as child nodes // if (shouldFilter(currentNode)) { attachAttributeList(currentNode, atts); previousNode.add(currentNode); } } /* * DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(lName); * * if (currentNode == null) { rootNode = newNode; } else { * if(shouldFilter(newNode)) * * * currentNode.add(newNode); } currentNode = newNode; * * } */ /* * private void filterNodes(DefaultMutableTreeNode node) { * System.out.println(node); int childCount = node.getChildCount(); for (int * i = childCount - 1; i >= 0; i–) { DefaultMutableTreeNode child = * (DefaultMutableTreeNode) node .getChildAt(i); if (shouldFilter(child)) { * node.remove(i); } else { filterNodes(child); } */ public void endElement(String uri, String qName, String lName) { if (currentNode.getUserObject().equals(lName)){ currentNode = (DefaultMutableTreeNode) currentNode.getParent(); } } private String getNodeText(final String lName, final Attributes atts) { final String postfix; if (“Cube”.equals(lName) && atts.getValue(“name”) != null) postfix = “: " + atts.getValue(“name”); else if (“Dimension”.equals(lName) && atts.getValue(“name”) != null) postfix = " :” + atts.getValue(“name”); else if (“AggregateTable”.equals(lName) && atts.getValue(“name”) != null) postfix = " :" + atts.getValue(“name”); else if (“PrimaryKey”.equals(lName) && atts.getValue(“name”) != null) postfix = " :" + atts.getValue(“name”); else if (“Measure”.equals(lName) && atts.getValue(“name”) != null) postfix = " :" + atts.getValue(“name”); else if (“FactDimension”.equals(lName) && atts.getValue(“name”) != null) postfix = " :" + atts.getValue(“name”); /* * else if (“AggregateLevel”.equals(lName) && atts.getValue(“name”) != * null) postfix = " :" + atts.getValue(“name”); */ else postfix = “”; return lName + postfix; } public DefaultMutableTreeNode getTree() { return rootNode; } boolean shouldFilter(DefaultMutableTreeNode node) { String name = (String) node.getUserObject(); /* System.out.println(name); */ return ((name.contains(“Cube”))); } private void attachAttributeList(DefaultMutableTreeNode node, Attributes atts) { for (int i = 0; i < atts.getLength(); i++) { String name = atts.getLocalName(i); String value = atts.getValue(name); if (!name.equals(“name”)) node.add(new DefaultMutableTreeNode(name + " = " + value)); } } }

                                                                          - krishna

                                                                            JournalDev
                                                                            DigitalOcean Employee
                                                                            DigitalOcean Employee badge
                                                                            January 23, 2020

                                                                            Hi Pankaj, Thanks for the content. In our application we are using SAXParser for parsing XML document and now we want to extend our functionality to json support as well. Can you suggest which is the best parser available performance vise as well as consistent with the XML’s SAXParser. Your quick reply on this is appreciated. Thanks, Krutik

                                                                            - Krutik Maheta

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              April 11, 2020

                                                                              Hi Pankaj I am a java developer ,I have been reading your documentation and following it since long time it has been a very helpfull for me ,now in one of our requirements we need to parse maven pom file in the java class to check all it dependencies ,so can you please help me with the soggestion for parsing pom file with skipping some of dependencies tags

                                                                              - Raghavendra Joshi

                                                                                JournalDev
                                                                                DigitalOcean Employee
                                                                                DigitalOcean Employee badge
                                                                                May 26, 2020

                                                                                I have an XML file as List in the input. How can I use SAX to parse the List? I don’t have access to the XML file directly. The list will be holding the line by line strings for XML.

                                                                                - Sam

                                                                                  JournalDev
                                                                                  DigitalOcean Employee
                                                                                  DigitalOcean Employee badge
                                                                                  July 1, 2021

                                                                                  Hi, Pankaj your article really helped me thankyou for posting such amazing content

                                                                                  - shashi kumar reddy

                                                                                    Try DigitalOcean for free

                                                                                    Click below to sign up and get $200 of credit to try our products over 60 days!

                                                                                    Sign up

                                                                                    Join the Tech Talk
                                                                                    Success! Thank you! Please check your email for further details.

                                                                                    Please complete your information!

                                                                                    Become a contributor for community

                                                                                    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                                                    DigitalOcean Documentation

                                                                                    Full documentation for every DigitalOcean product.

                                                                                    Resources for startups and SMBs

                                                                                    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                                                    Get our newsletter

                                                                                    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                                                    New accounts only. By submitting your email you agree to our Privacy Policy

                                                                                    The developer cloud

                                                                                    Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                                                    Get started for free

                                                                                    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                                                    *This promotional offer applies to new accounts only.