?? saxreader.java
字號:
package xmlwriter;import java.io.File;import java.io.IOException;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 SAXReader extends DefaultHandler{ java.util.Stack tags = new java.util.Stack(); //--------------XML Content------------- String text = null; String url = null; String author = null; String description = null; String day = null; String year = null; String month = null; //---------------------------------------------- public void endDocument() throws SAXException { System.out.println("------Parse End--------"); } public void startDocument() throws SAXException { System.out.println("------Parse Begin--------"); } public void startElement(String p0, String p1, String p2, Attributes p3) throws SAXException { tags.push(p1); } public void endElement(String p0, String p1, String p2) throws SAXException { tags.pop(); if (p1.equals("link")) printout(); } public void characters(char[] p0, int p1, int p2) throws SAXException { String tag = (String) tags.peek(); if (tag.equals("text")) text = new String(p0, p1, p2); else if (tag.equals("url")) url = new String(p0, p1, p2); else if (tag.equals("author")) author = new String(p0, p1, p2); else if (tag.equals("day")) day = new String(p0, p1, p2); else if (tag.equals("month")) month = new String(p0, p1, p2); else if (tag.equals("year")) year = new String(p0, p1, p2); else if (tag.equals("description")) year = new String(p0, p1, p2); } private void printout() { System.out.print("Content: "); System.out.println(text); System.out.print("URL: "); System.out.println(url); System.out.print("Author: "); System.out.println(author); System.out.print("Date: "); System.out.println(day + "-" + month + "-" + year); System.out.print("Description: "); System.out.println(description); System.out.println(); } static public void main(String[] args) { String filename = null; boolean validation = false; filename = "links.xml"; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxParser = null; try { saxParser = spf.newSAXParser(); } catch (Exception ex) { System.err.println(ex); System.exit(1); } try { saxParser.parse(new File(filename), new SAXReader()); } catch (SAXException se) { System.err.println(se.getMessage()); System.exit(1); } catch (IOException ioe) { System.err.println(ioe); System.exit(1); } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -