⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saxreader.java

📁 写xml文件的相关类,可以直接应用到项目
💻 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);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -