反射方式解析xml

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) {

            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值