Parsing will be accomplished utilizing two methods:
- Utilizing DOM Parser
- Utilizing SAX Parser
Learn XML file in Java Utilizing SAX Parser Instance
In DOM parser we have now seen that we have now to comply with easy three steps:
Ø Parse the XML file
Ø Create the java object
Ø Manipulate the article means we are able to learn that object or add them to record or no matter perform we would like we are able to do
However in SAX Parser its little bit totally different.
SAX Parser: it’s an event-based parsing it accommodates the default handler for dealing with the occasions at any time when SAX parser parses the xml doc and it finds the Begin tag “<” and finish tag”>” it calls corresponding handler methodology.
Pattern Instance of studying XML File – SAX Parser
Suppose we have now this pattern XML file financial institution.xml which accommodates account particulars of all accounts in a hypothetical financial institution:
<?xml model=“1.0” encoding=“UTF-8”?>
<Financial institution>
<Account sort=“saving”>
<Id>1001</Id>
<Title>Jack Robinson</Title>
<Amt>10000</Amt>
</Account>
<Account sort=“present”>
<Id>1002</Id>
<Title>Sony Company</Title>
<Amt>1000000</Amt>
</Account>
</Financial institution>
1. Create the SAX parser and parse the XML file: On this step we are going to take one manufacturing unit occasion from SAXParserFactory to parse the xml file this manufacturing unit occasion in turns give us occasion of parser utilizing the parse() methodology will parse the Xml file.
2. Occasion Dealing with: when Sax Parser begins the parsing at any time when it founds the beginning or finish tag it’s going to invoke the corresponding occasion dealing with methodology which is public void startElement (…) and public void finish Aspect (…).
3. Register the occasions: The category extends the Default Handler class to pay attention for callback occasions and we register this handler to sax Parser to inform us for name again occasion
Let see java code for all these steps
To characterize knowledge from our pattern xml file we’d like one java area object referred to as Account:
package deal parser;
public class Account {
personal String title;
personal int id;
personal int amt;
personal String sort;
public Account() {
}
public Account(String title, int id, int amt, String sort) {
this.title = title;
this.amt = amt;
this.id = id;
this.sort = sort;
}
public int getAmt() {
return amt;
}
public void setAmt(int amt) {
this.amt = amt;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return title;
}
public void setName(String title) {
this.title = title;
}
public String getType() {
return sort;
}
public void setType(String sort) {
this.sort = sort;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(“Account Particulars – “);
sb.append(“Title:” + getName());
sb.append(“, “);
sb.append(“Kind:” + getType());
sb.append(“, “);
sb.append(“Id:” + getId());
sb.append(“, “);
sb.append(“Age:” + getAmt());
sb.append(“.”);
return sb.toString();
}
}
Pattern Code for implementing SAX parser in Java
package deal parser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
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 ReadXMLFileUsingSaxparser extends DefaultHandler {
personal Account acct;
personal String temp;
personal ArrayList<Account> accList = new ArrayList<Account>();
/** The principle methodology units issues up for parsing */
public static void important(String[] args) throws IOException, SAXException,
ParserConfigurationException {
//Create a “parser manufacturing unit” for creating SAX parsers
SAXParserFactory spfac = SAXParserFactory.newInstance();
//Now use the parser manufacturing unit to create a SAXParser object
SAXParser sp = spfac.newSAXParser();
//Create an occasion of this class; it defines all of the handler strategies
ReadXMLFileUsingSaxparser handler = new ReadXMLFileUsingSaxparser();
//Lastly, inform the parser to parse the enter and notify the handler
sp.parse(“financial institution.xml”, handler);
handler.readList();
}
/*
* When the parser encounters plain textual content (not XML components),
* it calls(this methodology, which accumulates them in a string buffer
*/
public void characters(char[] buffer, int begin, int size) {
temp = new String(buffer, begin, size);
}
/*
* Each time the parser encounters the start of a brand new factor,
* it calls this methodology, which resets the string buffer
*/
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
temp = “”;
if (qName.equalsIgnoreCase(“Account”)) {
acct = new Account();
acct.setType(attributes.getValue(“sort”));
}
}
/*
* When the parser encounters the top of a component, it calls this methodology
*/
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase(“Account”)) {
// add it to the record
accList.add(acct);
} else if (qName.equalsIgnoreCase(“Title”)) {
acct.setName(temp);
} else if (qName.equalsIgnoreCase(“Id”)) {
acct.setId(Integer.parseInt(temp));
} else if (qName.equalsIgnoreCase(“Amt”)) {
acct.setAmt(Integer.parseInt(temp));
}
}
personal void readList() {
System.out.println(“No of the accounts in financial institution ‘” + accList.measurement() + “‘.”);
Iterator<Account> it = accList.iterator();
whereas (it.hasNext()) {
System.out.println(it.subsequent().toString());
}
}
Output:
No of the accounts in financial institution ‘2’.
Account Particulars – Title:Jack Robinson, Kind:saving, Id:1001, Age:10000.
Account Particulars – Title:Sony Company, Kind:present, Id:1002, Age:1000000.
Benefit of SAX parser in Java:
Another Tutorial it’s possible you’ll like