| |
 |
|
J2Concept® JAVA GURU |
|
|
| |
|
|
|
| |
|
|
| |
|
|
| |
| Sample Practical Hands-on labs |
| (More than 20 Hands-on labs in the book !!) |
|
| |
| Hands-on Lab: Parsing XML Document with SAX |
| Author: Chart Krobtragolchai |
| Tool: NetBeans 5.0 IDE |
| Book Chapter Number: 6 of 12 |
| Book Chapter Name: SOAP and XML Processing APIs |
| (JAXP, JAXB, and SAAJ) |
| Book Topic: Simple API for XML (SAX) |
| User Level: Beginner - Intermediate |
| Book Availability: Open for Reservation !! |
| Pages in web: 1, 2, 3, 4, 5 |
|
|
|
|
|
| |
|
| |
|
|
| |
Hands-on Lab Step (Page 2): |
|
| |
|
|
| |
4. The "TrainReader.java" will be generated by NetBeans 5.0 IDE, and then type the below code. |
|
| |
|
|
| |
/*
* TrainReader.java
*/
import java.io.File;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.*;
import org.xml.sax.helpers.*;/**
* @author J2Concept®
*/
public class TrainReader extends DefaultHandler{
/** Creates a new instance of TrainReader */
public TrainReader() {
}
public static void main(String[] args)throws Exception{
System.out.println("Running train reader...");
TrainReader readerObj = new TrainReader();
readerObj.read(args[0]);
}
public void read(String fileName)throws Exception{
SAXParserFactory spfactory = SAXParserFactory.newInstance();
// Turn on validation, and turn on namespaces
spfactory.setValidating(true);
spfactory.setNamespaceAware(true);
SAXParser saxParser = spfactory.newSAXParser();
saxParser.parse(new File(fileName),this);
}
public void startDocument() throws SAXException{
System.out.println("Start of the train");
}
public void endDocument() throws SAXException{
System.out.println("End of the train");
}
} |
|
|
| |
|
|
| |
5. In the Projects palette, right-click at TrainReader. The Project Properties window will appear (see picture).
|
|
| |
Select "Run" in the Categories part, and then type "Train.xml" in the Arguments part. Click OK.
|
|
| |
|
|
| |

|
|
| |
|
|
| |
translated by J2Concept® |
|
| |
Copyright © 2006, J2Concept®. All rights reserved. |
|
| |
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
|
|
|
|
| |
|
|