中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java使用SAX解析xml

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
import java.io.File;
import java.util.LinkedList;
import java.util.List;

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 ParseXMLFileWithSAX extends DefaultHandler {

private StringBuffer buffer = new StringBuffer();

    private static String responseCode;
    private static String date;
    private static String title;

    private static Currency currency;
    private static Rates rates;

    public static void main(String[] args) throws Exception {

        DefaultHandler handler = new ParseXMLFileWithSAX();

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);

        SAXParser parser = factory.newSAXParser();

        parser.parse(new File("in.xml"), handler);

        System.out.println("Response Code:" + responseCode);
        System.out.println("Date:" + date);
        System.out.println("Title:" + title);
        System.out.println("Rates:");

        for (Currency curr : rates.currencies) {
            System.out.println("\tCode:" + curr.code + " - Rate:" + curr.rate);
        }

    }

    private static class Currency {
        public String code;
        public String rate;
    }

    private static class Rates {
        public List<Currency> currencies = new LinkedList<Currency>();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        buffer.setLength(0);

        if (qName.equals("response")) {
            responseCode = attributes.getValue("code");
        }
        else if (qName.equals("date")) {
            date = "";
        }
        else if (qName.equals("title")) {
            title = "";
        }
        else if (qName.equals("rates")) {
            rates = new Rates(); 
        }
        else if (qName.equals("currency")) {
            currency = new Currency(); 
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName)throws SAXException {

        if (qName.equals("date")) {
            date = buffer.toString();
        }
        else if (qName.equals("title")) {
            title = buffer.toString();
        }
        else if (qName.equals("currency")) {
            rates.currencies.add(currency);
        }
        else if (qName.equals("code")) {
            currency.code = buffer.toString();
        }
        else if (qName.equals("rate")) {
            currency.rate = buffer.toString();
        }

    }

    public void characters(char[] ch, int start, int length) {
        buffer.append(ch, start, length);
    }

}

輸入xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<response code="200">
    <date>2008-11-07</date>
    <title>Exchange rates for 2008-11-07</title>
    <rates>
        <currency>
            <code>EUR</code>
            <rate>1.220</rate>
        </currency>
        <currency>
            <code>USD</code>
            <rate>1.275</rate>
        </currency>
    </rates>
</response>

輸出:

Response Code:200
Date:2008-11-07
Title:Exchange rates for 2008-11-07
Rates:
    Code:EUR - Rate:1.0
    Code:USD - Rate:1.275600

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Jsp使用HttpSessionBindingListener實(shí)現(xiàn)在線人數(shù)記錄

下一篇:java實(shí)現(xiàn)Ping示例代碼