启动XML解析器

时间:2013-07-23 15:10:10

标签: java xml string parsing

我是Java / Android编程的新手,需要对我正在编写的部分代码提供一点帮助。我一直在关注" Parsing XML Data"文章没有成功。

来自" MainActivity.java"的意图将带有XML文本的字符串发送到我的" ParsingXMLStringActivity.java"我想解析。这是我成功完成活动的字符串。

<action><app>survo</app><parameters><id>5666</id><p_t>205</p_t></parameters></action>

现在如果在下面的#34; ParsingXMLStringActivity.java&#34;

中编写公共类XmlParser
public class ParsingXMLStringActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_parsing_xmlstring);

    // Get the message from the intent
    Intent intent = getIntent();
    String receivedXMLstring = intent.getStringExtra(MainActivity.Authorize.XML_STRING);
    System.out.println("STRING:"+receivedXMLstring);
    InputStream in_stream;
    try {
        in_stream = new ByteArrayInputStream(receivedXMLstring.getBytes("UTF-8"));
        System.out.println("STREAM:"+ in_stream);
        XmlParser XmlParser = new XmlParser();
        System.out.println("XmlParser:"+ XmlParser);

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} // OnCreate
    public class XmlParser {

    public final String ns = null;

    public List<Entry> parse (InputStream in_stream) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in_stream, null);
            parser.nextTag();
            return readAction(parser);
        } finally {
            in_stream.close();
        }           
    } // Public List Parse

    private List<Entry> readAction(XmlPullParser parser) throws XmlPullParserException, IOException {
        List<Entry> action = new ArrayList<Entry>();

        parser.require(XmlPullParser.START_TAG, ns, "action");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Starts by looking for the entry tag
            if (name.equals("action")) {
                action.add(readParameters(parser));
            } else {
                skip(parser);
            }
        }  
        System.out.println("ACTION: "+action);
        return action;
    } // Public List ReadAction

    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        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;
            }
        }
     } //Private Void Skip

    public class Entry {
        public final String id;
        public final String pt;

        private Entry(String id, String pt) {
            this.id = id;
            this.pt = pt;
        }
    } // public static class Entry


    private Entry readParameters(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, ns, "entry");
        String id = null;
        String pt = null;
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("id")) {
                id = readId(parser);
                System.out.println("ID: "+ id);
            } else if (name.equals("p_t")) {
                pt = readPT(parser);
                System.out.println("P_T: "+ pt);
            }
            else {
                skip(parser);
            }
        }
        return new Entry(id, pt);
    } // Private Entry

    // Processes title tags in the feed.
    private String readId(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "id");
        String id = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "id");
        System.out.println(id);
        return id;
    }
    private String readPT(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "p_t");
        String pt = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "p_t");
        System.out.println(pt);
        return pt;
    }
    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        System.out.println(result);
        return result;
    }

}

System.out.println("ID: "+ id);没有向LogCat提供任何信息,我设置了断点来检查解析器是否已经启动但似乎没有用我提供的字符串启动解析过程。

有人有想法,可以告诉我我错过了哪一部分吗?

亲切的问候,本

0 个答案:

没有答案