• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

java使用SAXxml的代码

武飞扬头像
PHP中文网
帮助24

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfgjhj
系列文章
更多 icon
同类精品
更多 icon
继续加载