import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import java.util.ArrayList;
import java.util.HashMap;
public class ParseingXMLWithSAX {
HashMap m = new HashMap();
ArrayList ar = null;
String key;
String filename = "xml2.txt";
public void getElementFromXML() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean length1 = false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.toUpperCase().equalsIgnoreCase("r")) {
}
if (qName.toUpperCase().equalsIgnoreCase("v")) {
length1 = true;
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.toUpperCase().equalsIgnoreCase("v")) {
length1 = false;
}
if (qName.toUpperCase().equalsIgnoreCase("r")) {
System.out.println("");
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
if (length1) {
System.out.print( new String(ch, start, length) + " |");
}
}
};
saxParser.parse(filename, handler);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[])
{
new ParseingXMLWithSAX().getElementFromXML();
}
}
Books |2003 |Bantam Books |309 |$4,742 |$1,298 |
John Wiley |& | Sons |271 |$4,726 |$1,320 |
McGraw Hill |114 |$4,382 |$1,182 |
Perigee |334 |$3,015 |$779 |
Prentice Hall |186 |$2,370 |$637 |
Scribner |325 |$3,265 |$780 |
Simon |& | Schuster |214 |$3,218 |$870 |
Vintage Books |287 |$3,321 |$860 |
Warner Books |317 |$4,190 |$1,103 |
2004 |Bantam Books |275 |$4,327 |$1,176 |
John Wiley |& | Sons |206 |$3,468 |$968 |
McGraw Hill |92 |$3,843 |$1,035 |
Perigee |315 |$2,839 |$728 |
Prentice Hall |181 |$2,425 |$650 |
Scribner |311 |$3,139 |$751 |
Simon |& | Schuster |246 |$3,765 |$1,014 |
Vintage Books |278 |$3,234 |$827 |
Warner Books |288 |$3,952 |$1,069 |
Electronics |2003 |ACS Innovations |4,679 |$2,237,510 |$618,668 |
ATF Electronics |577 |$445,700 |$115,648 |
Audiotronics Inc. |2,027 |$407,290 |$109,823 |
Digital Equipment |756 |$64,960 |$17,436 |
Digital Office Inc. |789 |$269,030 |$71,177 |
DSS Appliance Co. |2,247 |$1,101,450 |$305,311 |
Entertaintron Inc. |2,180 |$597,860 |$162,497 |
Impact Components |1,029 |$188,253 |$51,574 |
MegaStore Corp. |819 |$423,213 |$116,456 |
Universal EL |478 |$217,718 |$60,335 |
2004 |ACS Innovations |4,042 |$1,858,920 |$509,858 |
ATF Electronics |598 |$462,500 |$119,981 |
Audiotronics Inc. |1,913 |$380,770 |$102,612 |
Digital Equipment |713 |$58,380 |$15,651 |
Digital Office Inc. |798 |$278,570 |$73,694 |
DSS Appliance Co. |2,086 |$1,036,450 |$287,678 |
Entertaintron Inc. |1,948 |$545,730 |$148,426 |
Impact Components |910 |$161,267 |$44,378 |
MegaStore Corp. |725 |$375,439 |$102,923 |
Universal EL |435 |$190,644 |$52,739 |
Movies |2003 |20th Century Fox |12,841 |$222,671 |$55,661 |
A |& |E Entertainment |4,892 |$127,721 |$33,686 |
Columbia Pictures |15,315 |$185,855 |$45,249 |
Disney Studios |9,500 |$160,446 |$40,374 |
Lyons Group |6,414 |$78,758 |$18,691 |
MGM Studios |8,818 |$117,280 |$27,750 |
Paramount Pictures |8,533 |$126,180 |$30,731 |
Sport Vision |966 |$18,993 |$4,878 |
TriStar Pictures |13,716 |$200,425 |$48,996 |
Universal Studios |11,861 |$175,516 |$43,631 |
Ventura Distribution |1,163 |$25,371 |$6,285 |
2004 |20th Century Fox |12,204 |$212,730 |$53,225 |
A |& |E Entertainment |4,628 |$120,891 |$31,856 |
Columbia Pictures |14,111 |$171,889 |$41,901 |
Disney Studios |8,727 |$147,355 |$37,042 |
Lyons Group |5,846 |$72,062 |$17,095 |
MGM Studios |7,989 |$106,029 |$25,096 |
Paramount Pictures |7,817 |$115,450 |$28,129 |
Sport Vision |917 |$17,654 |$4,515 |
TriStar Pictures |12,981 |$189,755 |$46,436 |
Universal Studios |10,804 |$160,960 |$40,143 |
Ventura Distribution |1,062 |$24,234 |$6,030 |
Music |2003 |BMG |23,954 |$331,871 |$29,541 |
Columbia House |6,788 |$91,776 |$9,148 |
Polygram |16,608 |$254,615 |$31,212 |
Sony Music |22,574 |$323,576 |$33,592 |
Virgin Records |18,716 |$284,133 |$33,905 |
WEA |11,284 |$165,230 |$22,617 |
2004 |BMG |22,257 |$307,634 |$27,357 |
Columbia House |6,350 |$86,428 |$8,725 |
Polygram |15,447 |$238,817 |$29,461 |
Sony Music |20,696 |$297,124 |$30,800 |
Virgin Records |17,381 |$263,071 |$30,997 |
WEA |10,071 |$148,059 |$20,441 |
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
/*
<product>
<name>laptop</name>
<price>120</price>
<maker>Dell</maker>
</product>
*/
import java.util.ArrayList;
import java.util.HashMap;
public class ParseingXMLWithSAX {
HashMap m = new HashMap();
ArrayList<String> ar = null;
String s;
String key;
String filename = "xml2.txt";
public void getElementFromXML() {
try {
ar = new ArrayList<String>();
s= "";
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean length1 = false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.toUpperCase().equalsIgnoreCase("r")) {
}
if (qName.toUpperCase().equalsIgnoreCase("v")) {
length1 = true;
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.toUpperCase().equalsIgnoreCase("v")) {
length1 = false;
}
if (qName.toUpperCase().equalsIgnoreCase("r")) {
// System.out.println("");
ar.add(s);
s="";
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
if (length1) {
// System.out.print( new String(ch, start, length) + " |");
s+= new String(ch, start, length) + " |";
}
}
};
saxParser.parse(filename, handler);
for(String s: ar){
s = s.replace("|& |"," & ");
System.out.println(s);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[])
{
new ParseingXMLWithSAX().getElementFromXML();
}
}
Books |2003 |Bantam Books |309 |$4,742 |$1,298 |
John Wiley & Sons |271 |$4,726 |$1,320 |
McGraw Hill |114 |$4,382 |$1,182 |
Perigee |334 |$3,015 |$779 |
Prentice Hall |186 |$2,370 |$637 |
Scribner |325 |$3,265 |$780 |
Simon & Schuster |214 |$3,218 |$870 |
Vintage Books |287 |$3,321 |$860 |
Warner Books |317 |$4,190 |$1,103 |
2004 |Bantam Books |275 |$4,327 |$1,176 |
John Wiley & Sons |206 |$3,468 |$968 |
McGraw Hill |92 |$3,843 |$1,035 |
Perigee |315 |$2,839 |$728 |
Prentice Hall |181 |$2,425 |$650 |
Scribner |311 |$3,139 |$751 |
Simon & Schuster |246 |$3,765 |$1,014 |
Vintage Books |278 |$3,234 |$827 |
Warner Books |288 |$3,952 |$1,069 |
Electronics |2003 |ACS Innovations |4,679 |$2,237,510 |$618,668 |
ATF Electronics |577 |$445,700 |$115,648 |
Audiotronics Inc. |2,027 |$407,290 |$109,823 |
Digital Equipment |756 |$64,960 |$17,436 |
Digital Office Inc. |789 |$269,030 |$71,177 |
DSS Appliance Co. |2,247 |$1,101,450 |$305,311 |
Entertaintron Inc. |2,180 |$597,860 |$162,497 |
Impact Components |1,029 |$188,253 |$51,574 |
MegaStore Corp. |819 |$423,213 |$116,456 |
Universal EL |478 |$217,718 |$60,335 |
2004 |ACS Innovations |4,042 |$1,858,920 |$509,858 |
ATF Electronics |598 |$462,500 |$119,981 |
Audiotronics Inc. |1,913 |$380,770 |$102,612 |
Digital Equipment |713 |$58,380 |$15,651 |
Digital Office Inc. |798 |$278,570 |$73,694 |
DSS Appliance Co. |2,086 |$1,036,450 |$287,678 |
Entertaintron Inc. |1,948 |$545,730 |$148,426 |
Impact Components |910 |$161,267 |$44,378 |
MegaStore Corp. |725 |$375,439 |$102,923 |
Universal EL |435 |$190,644 |$52,739 |
Movies |2003 |20th Century Fox |12,841 |$222,671 |$55,661 |
A & E Entertainment |4,892 |$127,721 |$33,686 |
Columbia Pictures |15,315 |$185,855 |$45,249 |
Disney Studios |9,500 |$160,446 |$40,374 |
Lyons Group |6,414 |$78,758 |$18,691 |
MGM Studios |8,818 |$117,280 |$27,750 |
Paramount Pictures |8,533 |$126,180 |$30,731 |
Sport Vision |966 |$18,993 |$4,878 |
TriStar Pictures |13,716 |$200,425 |$48,996 |
Universal Studios |11,861 |$175,516 |$43,631 |
Ventura Distribution |1,163 |$25,371 |$6,285 |
2004 |20th Century Fox |12,204 |$212,730 |$53,225 |
A & E Entertainment |4,628 |$120,891 |$31,856 |
Columbia Pictures |14,111 |$171,889 |$41,901 |
Disney Studios |8,727 |$147,355 |$37,042 |
Lyons Group |5,846 |$72,062 |$17,095 |
MGM Studios |7,989 |$106,029 |$25,096 |
Paramount Pictures |7,817 |$115,450 |$28,129 |
Sport Vision |917 |$17,654 |$4,515 |
TriStar Pictures |12,981 |$189,755 |$46,436 |
Universal Studios |10,804 |$160,960 |$40,143 |
Ventura Distribution |1,062 |$24,234 |$6,030 |
Music |2003 |BMG |23,954 |$331,871 |$29,541 |
Columbia House |6,788 |$91,776 |$9,148 |
Polygram |16,608 |$254,615 |$31,212 |
Sony Music |22,574 |$323,576 |$33,592 |
Virgin Records |18,716 |$284,133 |$33,905 |
WEA |11,284 |$165,230 |$22,617 |
2004 |BMG |22,257 |$307,634 |$27,357 |
Columbia House |6,350 |$86,428 |$8,725 |
Polygram |15,447 |$238,817 |$29,461 |
Sony Music |20,696 |$297,124 |$30,800 |
Virgin Records |17,381 |$263,071 |$30,997 |
WEA |10,071 |$148,059 |$20,441 |
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import java.util.ArrayList;
import java.util.HashMap;
public class ParseingXMLWithSAX {
HashMap m = new HashMap();
ArrayList<String> ar = null;
ArrayList<String> ar1 = null;
String s;
String key;
String filename = "xml2.txt";
public void getElementFromXML() {
try {
ar = new ArrayList<String>();
ar1 = new ArrayList<String>();
s= "";
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean length1 = false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.toUpperCase().equalsIgnoreCase("r")) {
}
if (qName.toUpperCase().equalsIgnoreCase("v")) {
length1 = true;
}
if (qName.toUpperCase().equalsIgnoreCase("attribute")) {
ar1.add(attributes.getValue("name"));
}
if (qName.toUpperCase().equalsIgnoreCase("metric")) {
ar1.add(attributes.getValue("name"));
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.toUpperCase().equalsIgnoreCase("v")) {
length1 = false;
}
if (qName.toUpperCase().equalsIgnoreCase("r")) {
// System.out.println("");
ar.add(s);
s="";
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
if (length1) {
// System.out.print( new String(ch, start, length) + " |");
s+= new String(ch, start, length) + " |";
}
}
};
saxParser.parse(filename, handler);
String s0="";
for(String s: ar1){
s0 += s + " |";
}
System.out.println(s0.substring(0,s0.length()-1));
// System.out.println("");
for(String s: ar){
s = s.replace("|& |"," & ");
System.out.println(s.substring(0,s.length()-1));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[])
{
new ParseingXMLWithSAX().getElementFromXML();
}
}
Category |Year |Supplier |Units Sold |Revenue |Profit
Books |2003 |Bantam Books |309 |$4,742 |$1,298
John Wiley & Sons |271 |$4,726 |$1,320
McGraw Hill |114 |$4,382 |$1,182
Perigee |334 |$3,015 |$779
Prentice Hall |186 |$2,370 |$637
Scribner |325 |$3,265 |$780
Simon & Schuster |214 |$3,218 |$870
Vintage Books |287 |$3,321 |$860
Warner Books |317 |$4,190 |$1,103
2004 |Bantam Books |275 |$4,327 |$1,176
John Wiley & Sons |206 |$3,468 |$968
McGraw Hill |92 |$3,843 |$1,035
Perigee |315 |$2,839 |$728
Prentice Hall |181 |$2,425 |$650
Scribner |311 |$3,139 |$751
Simon & Schuster |246 |$3,765 |$1,014
Vintage Books |278 |$3,234 |$827
Warner Books |288 |$3,952 |$1,069
Electronics |2003 |ACS Innovations |4,679 |$2,237,510 |$618,668
ATF Electronics |577 |$445,700 |$115,648
Audiotronics Inc. |2,027 |$407,290 |$109,823
Digital Equipment |756 |$64,960 |$17,436
Digital Office Inc. |789 |$269,030 |$71,177
DSS Appliance Co. |2,247 |$1,101,450 |$305,311
Entertaintron Inc. |2,180 |$597,860 |$162,497
Impact Components |1,029 |$188,253 |$51,574
MegaStore Corp. |819 |$423,213 |$116,456
Universal EL |478 |$217,718 |$60,335
2004 |ACS Innovations |4,042 |$1,858,920 |$509,858
ATF Electronics |598 |$462,500 |$119,981
Audiotronics Inc. |1,913 |$380,770 |$102,612
Digital Equipment |713 |$58,380 |$15,651
Digital Office Inc. |798 |$278,570 |$73,694
DSS Appliance Co. |2,086 |$1,036,450 |$287,678
Entertaintron Inc. |1,948 |$545,730 |$148,426
Impact Components |910 |$161,267 |$44,378
MegaStore Corp. |725 |$375,439 |$102,923
Universal EL |435 |$190,644 |$52,739
Movies |2003 |20th Century Fox |12,841 |$222,671 |$55,661
A & E Entertainment |4,892 |$127,721 |$33,686
Columbia Pictures |15,315 |$185,855 |$45,249
Disney Studios |9,500 |$160,446 |$40,374
Lyons Group |6,414 |$78,758 |$18,691
MGM Studios |8,818 |$117,280 |$27,750
Paramount Pictures |8,533 |$126,180 |$30,731
Sport Vision |966 |$18,993 |$4,878
TriStar Pictures |13,716 |$200,425 |$48,996
Universal Studios |11,861 |$175,516 |$43,631
Ventura Distribution |1,163 |$25,371 |$6,285
2004 |20th Century Fox |12,204 |$212,730 |$53,225
A & E Entertainment |4,628 |$120,891 |$31,856
Columbia Pictures |14,111 |$171,889 |$41,901
Disney Studios |8,727 |$147,355 |$37,042
Lyons Group |5,846 |$72,062 |$17,095
MGM Studios |7,989 |$106,029 |$25,096
Paramount Pictures |7,817 |$115,450 |$28,129
Sport Vision |917 |$17,654 |$4,515
TriStar Pictures |12,981 |$189,755 |$46,436
Universal Studios |10,804 |$160,960 |$40,143
Ventura Distribution |1,062 |$24,234 |$6,030
Music |2003 |BMG |23,954 |$331,871 |$29,541
Columbia House |6,788 |$91,776 |$9,148
Polygram |16,608 |$254,615 |$31,212
Sony Music |22,574 |$323,576 |$33,592
Virgin Records |18,716 |$284,133 |$33,905
WEA |11,284 |$165,230 |$22,617
2004 |BMG |22,257 |$307,634 |$27,357
Columbia House |6,350 |$86,428 |$8,725
Polygram |15,447 |$238,817 |$29,461
Sony Music |20,696 |$297,124 |$30,800
Virgin Records |17,381 |$263,071 |$30,997
WEA |10,071 |$148,059 |$20,441
Open in new window
Output:
Open in new window