其他分享
首页 > 其他分享> > Android使用XmlPullParser解析xml数据

Android使用XmlPullParser解析xml数据

作者:互联网

<?xml version="1.0" encoding="utf-8"?>
<one>
<title type="text">lallalalallala</title>
    <two>
    <title type="text">Where is my data file?</title>
    <link rel="alternate" href="http:www.xx.com" />
    <summary type="html">
        dodododoodoododo

    </summary>
    </two>

</one>
try {
            InputStream in = null;//要解析的文件的输入流
            final String ns = null;//不使用命名空间
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            //解析的第一个根节点
            parser.require(XmlPullParser.START_TAG, ns, "one");
            while (parser.next() != XmlPullParser.END_TAG) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                // 跳过其他节点,选择要解析的内部节点
                if (name.equals("two")) {
                    parser.require(XmlPullParser.START_TAG, ns, "two");
                    while (parser.next() != XmlPullParser.END_TAG) {
                        if (parser.getEventType() != XmlPullParser.START_TAG) {
                            continue;
                        }
                        String name1 = parser.getName();
                        if (name1.equals("title")) {
                            parser.require(XmlPullParser.START_TAG, ns, "title");
                            String result = "";
                            if (parser.next() == XmlPullParser.TEXT) {
                                result = parser.getText();
                                System.out.println("解析没有子标签的文本标签: "+result);
                                parser.nextTag();
                            }
                            parser.require(XmlPullParser.END_TAG, ns, "title");
                        } else if (name1.equals("summary")) {
                            parser.require(XmlPullParser.START_TAG, ns, "summary");
                            String result = "";
                            if (parser.next() == XmlPullParser.TEXT) {
                                result = parser.getText();
                                System.out.println("解析有子标签的但子标签里面有文本标签: "+result);
                                parser.nextTag();
                            }
                            parser.require(XmlPullParser.END_TAG, ns, "summary");
                        } else if (name1.equals("link")) {
                            String link = "";
                            parser.require(XmlPullParser.START_TAG, ns, "link");
                            String tag = parser.getName();
                            String relType = parser.getAttributeValue(null, "rel");
                            if (tag.equals("link")) {
                                if (relType.equals("alternate")){
                                    link = parser.getAttributeValue(null, "href");
                                    System.out.println("获取属性 "+link);
                                    parser.nextTag();
                                }
                            }
                            parser.require(XmlPullParser.END_TAG, ns, "link");
                        } else {
                            //跳过其他节点
                            if (parser.getEventType() != XmlPullParser.START_TAG) {
                                throw new IllegalStateException();
                            }
                            int depth = 1;
                            while (depth != 0) {
                                switch (parser.next()) {
                                    case XmlPullParser.END_TAG:
                                        depth--;
                                        break;
                                    case XmlPullParser.START_TAG:
                                        depth++;
                                        break;
                                }
                            }
                        }
                    }
                } else {
                    //跳过其他节点
                    if (parser.getEventType() != XmlPullParser.START_TAG) {
                        throw new IllegalStateException();
                    }
                    int depth = 1;
                    while (depth != 0) {
                        switch (parser.next()) {
                            case XmlPullParser.END_TAG:
                                depth--;
                                break;
                            case XmlPullParser.START_TAG:
                                depth++;
                                break;
                        }
                    }
                }
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

标签:xml,String,parser,START,XmlPullParser,TAG,Android,ns
来源: https://www.cnblogs.com/maowuge/p/16572904.html