public class Rss {
private float version;
private Channel channel;
public float getVersion() {
return version;
}
public void setVersion(float version) {
this.version = version;
}
public Channel getChannel() {
return channel;
}
public void setChannel(Channel channel) {
this.channel = channel;
}
}
public class Channel {
// <title>Nexus中文网</title>
//<link>http://www.inexus.co/portal.php</link>
//<description>Latest 20 articles of all categories</description>
private String title;
private String link;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public class XmlHttpDemo {
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.inexus.co/portal.php?mod=rss&catid=")
.get()
.build();
Call call = client.newCall(request);
try {
Response response = call.execute();
if (response.isSuccessful()) {
// System.out.println(response.body().string());
Rss rss = parseXml(response.body().charStream(), Rss.class);
System.out.println(rss.getVersion());
System.out.println(rss.getChannel().getTitle());
} else {
System.out.println(response.code());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static<T> T parseXml(Reader reader, Class<T> type) {
try {
T t = type.newInstance();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(reader);
Stack<Object> stack = new Stack<>();
int eleType;
while ((eleType = parser.next()) != XmlPullParser.END_DOCUMENT) {
switch (eleType) {
case XmlPullParser.START_TAG:
if (stack.isEmpty()) {
stack.push(t);
setAttribute(parser, stack);
} else {
try {
Field field = stack.peek().getClass().getDeclaredField(parser.getName());
Class<?> fieldType = field.getType();//获取属性的数据类型
if (fieldType.equals(String.class)) {
field.setAccessible(true);//暴力链接类中的属性
field.set(stack.peek(), parser.nextText());
} else if (fieldType.equals(Float.TYPE)) {
field.setAccessible(true);
field.set(stack.peek(), Float.parseFloat(parser.nextText()));
} else {
Object o = fieldType.newInstance();
stack.push(o);
setAttribute(parser, stack);
}
} catch (Exception e) {
stack.push(new Object());
}
}
break;
case XmlPullParser.END_TAG:
Object pop = stack.pop();
if (!stack.isEmpty()) {
try {
Object peek = stack.peek();
if (!peek.getClass().equals(Object.class)) {
Field field = peek.getClass().getDeclaredField(parser.getName());
field.setAccessible(true);
field.set(peek, pop);
}
} catch (Exception e) {
}
}
break;
}
}
return t;
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static void setAttribute(XmlPullParser parser, Stack<Object> stack) {
for (int i = 0; i < parser.getAttributeCount(); i++) {
try {
Field field = stack.peek().getClass().getDeclaredField(parser.getAttributeName(i));//得到parser的属性名字。
Class<?> fieldType = field.getType();
field.setAccessible(true);
if (fieldType.equals(String.class)) {
field.set(stack.peek(), parser.getAttributeValue(i));
} else if (fieldType.equals(Float.TYPE)) {
field.set(stack.peek(), Float.parseFloat(parser.getAttributeValue(i)));//得到parser的属性值。
}
} catch (Exception e) {
}
}
}
}