/ 1. creating instance of parser XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser pp = factory.newPullParser(); // 2. setting options // ex. disabling mixed content for elements // (element can not have elements mixed with non-whitespace string content) pp.setAllowedMixedContent(false); // 3. setting input String data = "<hello>World!</hello>"; // input will be taken from java.io.Reader pp.setInput(new StringReader(data)); // input could be also taken from String directly: //pp.setInput(data.toCharArray()); // 4. parsing //declare variables used during parsing byte type; // received event type XmlStartTag stag = factory.newStartTag(); XmlEndTag etag = factory.newEndTag(); // start parsing loop while((type = pp.next()) != XmlPullParser.END_DOCUMENT) { if(type == XmlPullParser.CONTENT) { String s = pp.readContent(); System.out.println("CONTENT={'"+s+"'}"); } else if(type == XmlPullParser.END_TAG) { pp.readEndTag(etag); System.out.println(""+etag); } else if(type == XmlPullParser.START_TAG) { pp.readStartTag(stag); System.out.println(""+stag); } }
xml pp2
最新推荐文章于 2022-03-11 14:47:50 发布