国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[JAVA100例]069、使用SAX解析XML

2019-11-18 13:55:51
字體:
來源:轉載
供稿:網友

ReadxmlDemo.java


import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;


public class ReadXmlDemo extends DefaultHandler {


    public static void main(String[] arguments) {
        if (arguments.length > 0){
     ReadXmlDemo read = new ReadXmlDemo(arguments[0]);
 } else {
     System.out. }
    }


    ReadXmlDemo(String xmlFile) {
        File input = new File(xmlFile);
 SAXParserFactory factory = SAXParserFactory.newInstance();
 factory.setValidating(true);
 try {
     SAXParser sax = factory.newSAXParser();
     sax.parse(input, new XmlHandler() );
 } catch (ParserConfigurationException pce) {
     System.out.println("Could not create that parser.");
     System.out.println(pce.getMessage());
 } catch (SAXException saxe) {
     System.out.println("Problem with the SAX parser.");
     System.out.println(saxe.getMessage());
 } catch (IOException ioe) {
     System.out.println("Error reading file.");
     System.out.println(ioe.getMessage());
 }
    }
}


class XmlHandler extends DefaultHandler {
    static int READING_NAME = 1;
    static int READING_SEX = 2;
    static int READING_BIRTH = 3;
    static int READING_NOTHING = 0;
    int currentActivity = READING_NOTHING;
    ReadXml xml = new ReadXml();


    XmlHandler() {
        super();
    }


    public void startElement(String uri, String localName, String qName, Attributes attributes) {
   
        if (qName.equals("title"))
            currentActivity = READING_NAME;
 else if (qName.equals("file"))
     currentActivity = READING_SEX;
 //else if (qName.equals("Birth"))
     //currentActivity = READING_BIRTH;
    }


    public void characters(char[] ch, int start, int length) {
        String value = new String(ch, start, length);
 if (currentActivity == READING_NAME)
     xml.name = value;
 if (currentActivity == READING_SEX)
     xml.sex = value;
 //if (currentActivity == READING_BIRTH)
     //xml.birth = value;
    }




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜新市| 外汇| 梓潼县| 长宁县| 克拉玛依市| 姜堰市| 孟连| 迭部县| 云阳县| 手游| 大宁县| 金乡县| 和政县| 恩平市| 凌云县| 神木县| 靖边县| 宜丰县| 馆陶县| 昌宁县| 周宁县| 彰化县| 泸州市| 东阳市| 通渭县| 将乐县| 互助| 陈巴尔虎旗| 阿克| 南涧| 鸡泽县| 梨树县| 宜阳县| 尉氏县| 宜黄县| 冕宁县| 桓台县| 大悟县| 银川市| 墨脱县| 焉耆|