package com.main;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class SAXTest extends DefaultHandler {
private class Item{
private String title ;
private String description;
private String converter;
public String getConverter() {
return converter;
}
public void setConverter(String converter) {
this.converter = converter;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
private Map<String, Item> itemMap = new HashMap<String, Item>();
private Item item ;
private int currentstate = 0;
private final int TITLE = 2;
private final int DESCRIPTION = 3;
public Map<String, Item> readUrl(String uri){
try{
URL url = new URL(uri);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(this);
InputSource is = new InputSource(url.openStream());
xmlReader.parse(is);
}catch(Exception e){
e.printStackTrace();
}
return itemMap;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String str = new String(ch, start, length);
// System.out.println("+++++++++++++++++++currStr:"+str);
if(this.item==null){
return;
}
switch(this.currentstate){
case TITLE:
this.item.setTitle(str);
this.currentstate = 0;
break;
case DESCRIPTION:
this.item.setDescription(str);
this.currentstate = 0;
break;
default:
return;
}
}
public void endDocument() throws SAXException {
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// System.out.println("localName:"+localName+"------- name:"+name);
if("item".equals(name)){
if(this.item!=null){
this.itemMap.put(this.item.getTitle(), this.item);
}
this.item = new Item();
currentstate = 1;
return;
}
if("title".equals(name)){
currentstate = 2;
}
if("description".equals(name)){
currentstate = 3;
}
}
public static String getConverter(String description){
String converter = "";
String find = "//d{1,}(//.//d+|)";
Pattern p = Pattern.compile(find);
Matcher m = p.matcher(description);
if(m.find()){
converter = m.group();
}
return converter;
}
public static void main(String[] args) {
SAXTest sax = new SAXTest();
Map<String, Item> tempMap = sax.readUrl("http://themoneyconverter.com/AED/rss.xml");
int i = 0;
for(Entry<String, Item> entrySet : tempMap.entrySet()){
String key = entrySet.getKey();
Item item = entrySet.getValue();
item.setConverter(SAXTest.getConverter(item.getDescription().substring(item.getDescription().indexOf("=")==-1?0:item.getDescription().indexOf("="),item.getDescription().length())));
System.out.println("Description:"+item.getDescription());
System.out.println("转换:"+key+"---汇率:"+item.getConverter());
i++;
}
System.out.println("共有数据:"+i);
}
}
SAX操作
最新推荐文章于 2022-11-19 15:07:38 发布