|
 |
|
J2Concept® JAVA GURU |
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่างแบบฝึกหัด (Hands-on labs) |
(พบกับแบบฝึกหัดกว่า 20 ชุดจากหนังสือเล่มนี้ !!) |
|
|
Hands-on Lab: Parsing XML Document with SAX |
Author: ฉัตร ครบตระกูลชัย |
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: เริ่มต้น - ปานกลาง |
Book Availability: เปิดจองหนังสือแล้ว จองด่วน !! |
Pages in web: 1, 2, 3, 4, 5 |
|
|
|
|
|
|
|
|
|
|
|
Hands-on Lab Step (Page 2): |
|
|
|
|
|
4. หลังจากที่ NetBeans 5.0 IDE ทำงานเสร็จเรียบร้อยแล้ว จะเกิดหน้าต่าง TrainReader ขึ้นมา |
|
|
ให้ทำการพิมพ์ 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.ในหน้าต่าง Projects ให้ทำการคลิกขวาที่ TrainReader และเลือก Properties จะเกิดหน้าต่าง Project Properties ขึ้นมา
|
|
|
ในช่อง Categories เลือก Run จากนั้น ในช่อง Arguments ให้พิมพ์ Train.xml และคลิก OK
|
|
|
|
|
|

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