java使用SAX解析xml的代码实例
程序员文章站
2022-03-02 17:57:49
...
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
以上就是java使用SAX解析xml的代码实例的详细内容,更多请关注其它相关文章!
推荐阅读
-
java使用DOM对XML文档进行增删改查操作实例代码
-
Python通过DOM和SAX方式解析XML的应用实例分享
-
Python中使用SAX解析xml实例
-
java使用dom4j生成与解析xml文档的方法示例
-
Android编程使用sax解析xml数据的方法详解
-
浅谈Java代码的 微信长链转短链接口使用 post 请求封装Json(实例)
-
Java基础之XML介绍与SAX解析、DOM解析XML、JDOM解析、DOM4J解析、XMLEncoder与XMLDecoder的使用以及xstream工具的使用 189~195
-
Java编程线程同步工具Exchanger的使用实例解析
-
荐 Java中多线程的使用(超级超级详细)线程安全+线程锁原理解析+保证线程安全的三种方式 (同步代码块+同步方法+lock锁) 5
-
PHP用SAX解析XML的实现代码与问题分析