在Android中解析XML?

时间:2014-06-20 07:06:50

标签: android xml xml-parsing xmlpullparser

我有以下XML,

    <?xml version="1.0"?>

    <rss xmlns:Data="http://www.google.com" version="2.0">

    <Data:NoticeBoard Mesage="Welcome " Id="1"/>

    <Data:NoticeBoard Mesage="Hi......" Id="2"/>

    </rss>

我需要解析它并在&#34; Message&#34;中获取文本。和&#34; Id&#34;。

我尝试使用pull解析器,但我只获得了开始标记,即#34; Notice Board&#34;

这是我试过的代码

    public void parseXML(XmlPullParser myParser) {
    try {
        int event = myParser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            String name = myParser.getName();
            switch (event) {
            case XmlPullParser.START_TAG:
                System.out.println("Start tag " + myParser.getName());
                break;
            case XmlPullParser.END_TAG:
                System.out.println("End tag " + myParser.getName());
                break;
            case XmlPullParser.TEXT:
                System.out.println("Text " + myParser.getText());
                break;
            }
            event = myParser.next();
        }
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

3 个答案:

答案 0 :(得分:1)

这是解析代码

您需要的是获取您从未做过的属性值。您有myParser.getName()但是您需要属性Mesage的值。

public class XMLPullParserHandler {

    private String text;

    public XMLPullParserHandler() {

    }
    public Void parse(InputStream is) { // pass the input stream
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();

            parser.setInput(is, null);
            boolean check =false;

           //factory instantiates an object

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("NoticeBoard")) {

                        String value = parser.getAttributeValue(null, "Mesage"); 
                        Log.i("Value is.........",""+value);

                    }


                    break;

                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;

                case XmlPullParser.END_TAG:


                    break;

                default:
                    break;
                }
                eventType = parser.next();
            }

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

答案 1 :(得分:0)

您可以使用Dom4J来解析数据。

您可以在BeginnersBook中使用此示例:Parsing XML using Dom4J API

答案 2 :(得分:0)

终于做到了

     try {
        xmlFactoryObject = XmlPullParserFactory.newInstance();
        xmlFactoryObject.setNamespaceAware(true);
        myparser = xmlFactoryObject.newPullParser();

        myparser.setInput(new StringReader(apiResponse));
        int event;

        event = myparser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            if (event == XmlPullParser.START_DOCUMENT) {
            } else if (event == XmlPullParser.END_DOCUMENT) {
            } else if (event == XmlPullParser.START_TAG) {
                System.out.println("Start tag " + myparser.getName());
                if (myparser.getName().equalsIgnoreCase("NoticeBoard")) {
                    mesaage.add(myparser.getAttributeValue(1));
                    Log.e("", myparser.getAttributeValue(1));
                }
            } else if (event == XmlPullParser.END_TAG) {

            } else if (event == XmlPullParser.TEXT) {
            }
            event = myparser.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }