先来看一下需要解析的数据:
北京
101010100
北京
2016年08月18日18:00
2016-08-18 18:12:20
17:05
18℃
17%
东南风1级
void |
startDocument()
Receive notification of the beginning of the document.
|
void |
startElement(String uri, String localName, String qName,Attributes attributes)
Receive notification of the start of an element.
|
void |
characters(char[] ch, int start, int length)
Receive notification of character data inside an element.
|
void |
endElement(String uri, String localName, String qName)
Receive notification of the end of an element.
|
void |
endDocument()
Receive notification of the end of the document.
|
if (attributes.getQName(0).equals("author")) {// 设置标签中的属性
weatherinfo.setAuthor(attributes.getValue(0));
}
if (attributes.getQName(1).equals("date")) {
weatherinfo.setDate(attributes.getValue(1));
}
package com.hejingzhou.decodexml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XmlHandler extends DefaultHandler {
private Weatherinfo weatherinfo;
private String nodeName;
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("开始进入文档进行解析");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("weatherinfo")) {
weatherinfo = new Weatherinfo();
if (attributes.getQName(0).equals("author")) {// 设置标签中的属性
weatherinfo.setAuthor(attributes.getValue(0));
}
if (attributes.getQName(1).equals("date")) {
weatherinfo.setDate(attributes.getValue(1));
}
}
nodeName = qName;//将读到的元素头保存下来
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String info = new String(ch, start, length);
if (nodeName.equals("cityname")) {
weatherinfo.setCityname(info);
}
if (nodeName.equals("citycode")) {
weatherinfo.setCitycode(info);
}
if (nodeName.equals("citydesc")) {
weatherinfo.setCitydesc(info);
}
if (nodeName.equals("publishtime")) {
weatherinfo.setPublishtime(info);
}
if (nodeName.equals("lastupdate")) {
weatherinfo.setLastupdate(info);
}
if (nodeName.equals("updatetime")) {
weatherinfo.setUpdatetime(info);
}
if (nodeName.equals("temperature")) {
weatherinfo.setTemperature(info);
}
if (nodeName.equals("humidity")) {
weatherinfo.setHumidity(info);
}
if (nodeName.equals("winddirect")) {
weatherinfo.setWinddirect(info);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// 如果里面的数据位重复的时候,设置数组的时候,从这里可以判断末尾节点,然后就可以通过判断节点尾部字符进行设置add方法这样的话就不会进行重复添加
nodeName = "";//读取到元素尾部的标签时将 标签名称清空
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("结束文档的读取");
super.endDocument();
}
public Weatherinfo getWeatherinfo() {
return weatherinfo;
}
public void setWeatherinfo(Weatherinfo weatherinfo) {
this.weatherinfo = weatherinfo;
}
}
/**
* 采用SAX
*
* @param data
*/
private static void type$1(File data) {
// TODO Auto-generated method stub
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = factory.newSAXParser();
XmlHandler xmlHandler = new XmlHandler();
saxParser.parse(new File("Data.xml"), xmlHandler);
Weatherinfo weatherinfo = xmlHandler.getWeatherinfo();
System.out.println(weatherinfo);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
还是比较简单的,这样就可以解析出来这个数据并且放在这个类中。
/**
* XmlPull
*
* @param data
*/
private static void type$2(File data) {
// TODO Auto-generated method stub
XmlPullParserFactory factory;
try {
factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
// xmlPullParser.setInput(new FileReader("Data.xml"));
xmlPullParser.setInput(new FileInputStream("Data.xml"), "UTF-8");
Weatherinfo weatherinfo = null;
String tag = "";
int even = xmlPullParser.getEventType();
while (even != XmlPullParser.END_DOCUMENT) {
switch (even) {
case XmlPullParser.START_DOCUMENT:
System.out.println("进入文档开始解析");
break;
case XmlPullParser.START_TAG:
if (xmlPullParser.getName().equals("weatherinfo")) {
weatherinfo = new Weatherinfo();
// 在这里进行访问标签属性
weatherinfo.setAuthor(xmlPullParser.getAttributeValue(0));
weatherinfo.setDate(xmlPullParser.getAttributeValue(1));
}
tag = xmlPullParser.getName();
break;
case XmlPullParser.TEXT:
if (tag.equals("cityname")) {
weatherinfo.setCityname(xmlPullParser.getText());
}
if (tag.equals("citycode")) {
weatherinfo.setCitycode(xmlPullParser.getText());
}
if (tag.equals("citydesc")) {
weatherinfo.setCitydesc(xmlPullParser.getText());
}
if (tag.equals("publishtime")) {
weatherinfo.setPublishtime(xmlPullParser.getText());
}
if (tag.equals("lastupdate")) {
weatherinfo.setLastupdate(xmlPullParser.getText());
}
if (tag.equals("updatetime")) {
weatherinfo.setUpdatetime(xmlPullParser.getText());
}
if (tag.equals("temperature")) {
weatherinfo.setTemperature(xmlPullParser.getText());
}
if (tag.equals("humidity")) {
weatherinfo.setHumidity(xmlPullParser.getText());
}
if (tag.equals("winddirect")) {
weatherinfo.setWinddirect(xmlPullParser.getText());
}
break;
case XmlPullParser.END_TAG:
tag = "";
break;
default:
break;
}
try {
even = xmlPullParser.next();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(weatherinfo);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取数据
*
* @return
*/
private static File getData() {
File file = null;
// TODO Auto-generated method stub
try {
HttpURLConnection hConnection = (HttpURLConnection) new URL("http://localhost:8081/XmlService/data.xml")
.openConnection();
if (hConnection.getResponseCode() == 200) {
FileOutputStream fOutputStream = new FileOutputStream("Data.xml");
InputStream inputStream = hConnection.getInputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) != -1) {
fOutputStream.write(buffer, 0, len);
}
fOutputStream.close();
file = new File("Data.xml");
file.createNewFile();
} else {
System.out.println("访问服务器失败,错误代码: " + hConnection.getResponseCode());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}