java如何使用SAX解析xml

发布时间:2020-07-16 10:35:42 作者:Leah
来源:亿速云 阅读:140

java如何使用SAX解析xml?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. SAX如何解析XML
  2. 如何使用SAX方式解析XML文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java sax 解析xml

上一篇:postgresql连接数据库的方法

下一篇:如何使用JS+HTML5实现图片上传预览效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》