Java Code Examples for org.xmlpull.v1.XmlPullParser#nextText()
The following examples show how to use
org.xmlpull.v1.XmlPullParser#nextText() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Features.java From Spark with Apache License 2.0 | 6 votes |
@Override public Features parse( XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { Features features = new Features(); boolean done = false; while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG && "event".equals(parser.getName())) { parser.nextText(); } if (eventType == XmlPullParser.START_TAG && "feature".equals(parser.getName())) { String feature = parser.getAttributeValue("", "var"); features.addFeature(feature); } else if (eventType == XmlPullParser.END_TAG) { if ("event".equals(parser.getName())) { done = true; } } } return features; }
Example 2
Source File: ReceiptProvider.java From xyTalk-pc with GNU Affero General Public License v3.0 | 6 votes |
@Override public Receipt parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { Receipt receipt = new Receipt(); boolean done = false; while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG && "request".equals(parser.getName())) { parser.nextText(); } else if (eventType == XmlPullParser.END_TAG) { if ("request".equals(parser.getName())) { done = true; } } } return receipt; }
Example 3
Source File: Features.java From xyTalk-pc with GNU Affero General Public License v3.0 | 6 votes |
public Features parse( XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { Features features = new Features(); boolean done = false; while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG && "event".equals(parser.getName())) { parser.nextText(); } if (eventType == XmlPullParser.START_TAG && "feature".equals(parser.getName())) { String feature = parser.getAttributeValue("", "var"); features.addFeature(feature); } else if (eventType == XmlPullParser.END_TAG) { if ("event".equals(parser.getName())) { done = true; } } } return features; }
Example 4
Source File: PersistentDataStore.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private static int loadIntValue(XmlPullParser parser) throws IOException, XmlPullParserException { try { String value = parser.nextText(); return Integer.parseInt(value); } catch (NumberFormatException nfe) { return 0; } }
Example 5
Source File: PullSOAPActionProcessorImpl.java From DroidDLNA with GNU General Public License v3.0 | 5 votes |
protected ActionException readFaultElement(XmlPullParser xpp) throws Exception { // We're in the "Fault" tag String errorCode = null; String errorDescription = null; XmlPullParserUtils.searchTag(xpp, "UPnPError"); int event; do { event = xpp.next(); if (event == XmlPullParser.START_TAG) { String tag = xpp.getName(); if (tag.equals("errorCode")) { errorCode = xpp.nextText(); } else if (tag.equals("errorDescription")) { errorDescription = xpp.nextText(); } } } while (event != XmlPullParser.END_DOCUMENT && (event != XmlPullParser.END_TAG || !xpp.getName().equals("UPnPError"))); if (errorCode != null) { try { int numericCode = Integer.valueOf(errorCode); ErrorCode standardErrorCode = ErrorCode.getByCode(numericCode); if (standardErrorCode != null) { log.fine("Reading fault element: " + standardErrorCode.getCode() + " - " + errorDescription); return new ActionException(standardErrorCode, errorDescription, false); } else { log.fine("Reading fault element: " + numericCode + " - " + errorDescription); return new ActionException(numericCode, errorDescription); } } catch (NumberFormatException ex) { throw new RuntimeException("Error code was not a number"); } } throw new RuntimeException("Received fault element but no error code"); }
Example 6
Source File: Dsmlv2Grammar.java From directory-ldap-api with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void action( Dsmlv2Container container ) throws XmlPullParserException { ExtendedRequestDsml<?, ?> extendedRequest = ( ExtendedRequestDsml<?, ?> ) container.getBatchRequest().getCurrentRequest(); XmlPullParser xpp = container.getParser(); try { // We have to catch the type Attribute Value before going to the next Text node String typeValue = ParserUtils.getXsiTypeAttributeValue( xpp ); // Getting the value String nextText = xpp.nextText(); if ( !Strings.isEmpty( nextText ) ) { if ( ParserUtils.isBase64BinaryValue( xpp, typeValue ) ) { extendedRequest.setRequestValue( Base64.decode( nextText.trim().toCharArray() ) ); } else { extendedRequest.setRequestValue( Strings.getBytesUtf8( nextText.trim() ) ); } } } catch ( IOException ioe ) { throw new XmlPullParserException( I18n.err( I18n.ERR_03008_UNEXPECTED_ERROR, ioe.getMessage() ), xpp, ioe ); } }
Example 7
Source File: Dsmlv2ResponseGrammar.java From directory-ldap-api with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void action( Dsmlv2Container container ) throws XmlPullParserException { SearchResponse searchResponse = ( SearchResponse ) container.getBatchResponse().getCurrentResponse().getDecorated(); SearchResultEntryDsml searchResultEntry = searchResponse.getCurrentSearchResultEntry(); XmlPullParser xpp = container.getParser(); try { // We have to catch the type Attribute Value before going to the next Text node String typeValue = ParserUtils.getXsiTypeAttributeValue( xpp ); // Getting the value String nextText = xpp.nextText(); try { if ( ParserUtils.isBase64BinaryValue( xpp, typeValue ) ) { searchResultEntry.addAttributeValue( Base64.decode( nextText.toCharArray() ) ); } else { searchResultEntry.addAttributeValue( nextText ); } } catch ( LdapException le ) { throw new XmlPullParserException( le.getMessage(), xpp, le ); } } catch ( IOException ioe ) { throw new XmlPullParserException( I18n.err( I18n.ERR_03008_UNEXPECTED_ERROR, ioe.getMessage() ), xpp, ioe ); } }
Example 8
Source File: XMLParser.java From QPM with Apache License 2.0 | 5 votes |
private String safeNextText(XmlPullParser parser) { try { if (needReadText) { String result = parser.nextText(); // 解决PULL解析的BUG,在调用了nextText方法后,会默认跳过该END_TAG,需要手动触发 if (parser.getEventType() == XmlPullParser.END_TAG) { stopParse(parser); } return result; } } catch (XmlPullParserException | IOException e) { e.printStackTrace(); } return null; }
Example 9
Source File: GradleDependencyEntity.java From freeline with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * xml解析为对象 * * @param text * @return */ public static GradleDependencyEntity parse(String text) { GradleDependencyEntity entity = new GradleDependencyEntity(); XmlPullParserFactory f = null; try { f = XmlPullParserFactory.newInstance(); f.setNamespaceAware(true); XmlPullParser xmlPullParser = f.newPullParser(); xmlPullParser.setInput(new InputStreamReader(new ByteArrayInputStream(text.getBytes()))); int eventType = xmlPullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_DOCUMENT) { } else if (eventType == XmlPullParser.START_TAG) { String name = xmlPullParser.getName(); if (name.equals("groupId")) { entity.setGroupId(xmlPullParser.nextText()); } else if (name.equals("artifactId")) { entity.setArtifactId(xmlPullParser.nextText()); } else if (name.equals("version")) { String version = xmlPullParser.nextText(); entity.setVersion(version); } else if (name.equals("lastUpdated")) { entity.setUpdateTime(xmlPullParser.nextText()); } } else if (eventType == XmlPullParser.END_TAG) { } else if (eventType == XmlPullParser.TEXT) { } eventType = xmlPullParser.next(); } } catch (Exception e) { e.printStackTrace(); } return entity; }
Example 10
Source File: TtmlDecoder.java From Telegram with GNU General Public License v2.0 | 5 votes |
private void parseMetadata(XmlPullParser xmlParser, Map<String, String> imageMap) throws IOException, XmlPullParserException { do { xmlParser.next(); if (XmlPullParserUtil.isStartTag(xmlParser, TtmlNode.TAG_IMAGE)) { String id = XmlPullParserUtil.getAttributeValue(xmlParser, "id"); if (id != null) { String encodedBitmapData = xmlParser.nextText(); imageMap.put(id, encodedBitmapData); } } } while (!XmlPullParserUtil.isEndTag(xmlParser, TtmlNode.TAG_METADATA)); }
Example 11
Source File: UpdateXmlParser.java From android-autoupdater with Apache License 2.0 | 5 votes |
/** * Parse UpdateTips * * @param xpp * @return * @throws XmlPullParserException * @throws IOException */ private Map<String, String> parseUpdateTips(XmlPullParser xpp) throws XmlPullParserException, IOException { Map<String, String> updateTips = new HashMap<String, String>(); String currentTag = null; String currentValue = null; int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: currentTag = xpp.getName(); if (currentTag.equals(TAG_UPDATE_TIPS)) { updateTips = new HashMap<String, String>(); } else { currentValue = xpp.nextText(); updateTips.put(currentTag, currentValue); } break; case XmlPullParser.END_TAG: break; case XmlPullParser.TEXT: break; default: break; } eventType = xpp.next(); } return updateTips; }
Example 12
Source File: Dsmlv2Grammar.java From directory-ldap-api with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void action( Dsmlv2Container container ) throws XmlPullParserException { CompareRequest compareRequest = ( CompareRequest ) container.getBatchRequest().getCurrentRequest(); XmlPullParser xpp = container.getParser(); try { // We have to catch the type Attribute Value before going to the next Text node String typeValue = ParserUtils.getXsiTypeAttributeValue( xpp ); // Getting the value String nextText = xpp.nextText(); if ( !Strings.isEmpty( nextText ) ) { if ( ParserUtils.isBase64BinaryValue( xpp, typeValue ) ) { compareRequest.setAssertionValue( Base64.decode( nextText.trim().toCharArray() ) ); } else { compareRequest.setAssertionValue( nextText.trim() ); } } } catch ( IOException ioe ) { throw new XmlPullParserException( I18n.err( I18n.ERR_03008_UNEXPECTED_ERROR, ioe.getMessage() ), xpp, ioe ); } }
Example 13
Source File: XMLParser.java From light-novel-library_Wenku8_Android with GNU General Public License v2.0 | 4 votes |
static public NovelFullInfo getNovelFullInfo(String xml) { try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); NovelFullInfo nfi = null; xmlPullParser.setInput(new StringReader(xml)); int eventType = xmlPullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if ("metadata".equals(xmlPullParser.getName())) { nfi = new NovelFullInfo(); } else if ("data".equals(xmlPullParser.getName())) { if ("Title".equals(xmlPullParser.getAttributeValue(0))) { nfi.aid = new Integer( xmlPullParser.getAttributeValue(1)); nfi.title = xmlPullParser.nextText(); } else if ("Author".equals(xmlPullParser .getAttributeValue(0))) { nfi.author = xmlPullParser.getAttributeValue(1); } else if ("DayHitsCount".equals(xmlPullParser .getAttributeValue(0))) { nfi.dayHitsCount = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("TotalHitsCount".equals(xmlPullParser .getAttributeValue(0))) { nfi.totalHitsCount = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("PushCount".equals(xmlPullParser .getAttributeValue(0))) { nfi.pushCount = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("FavCount".equals(xmlPullParser .getAttributeValue(0))) { nfi.favCount = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("PressId".equals(xmlPullParser .getAttributeValue(0))) { nfi.pressId = xmlPullParser.getAttributeValue(1); } else if ("BookStatus".equals(xmlPullParser .getAttributeValue(0))) { nfi.bookStatus = xmlPullParser.getAttributeValue(1); } else if ("BookLength".equals(xmlPullParser .getAttributeValue(0))) { nfi.bookLength = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("LastUpdate".equals(xmlPullParser .getAttributeValue(0))) { nfi.lastUpdate = xmlPullParser.getAttributeValue(1); } else if ("LatestSection".equals(xmlPullParser .getAttributeValue(0))) { nfi.latestSectionCid = new Integer( xmlPullParser.getAttributeValue(1)); nfi.latestSectionName=xmlPullParser.nextText(); } } break; } eventType = xmlPullParser.next(); } return nfi; } catch (Exception e) { e.printStackTrace(); return null; } }
Example 14
Source File: Wenku8Parser.java From light-novel-library_Wenku8_Android with GNU General Public License v2.0 | 4 votes |
/** * save the new xsl into an existing review reply list * @param reviewReplyList the existing review reply list object * @param xml the fetched xml */ static public void parseReviewReplyList(ReviewReplyList reviewReplyList, String xml) { reviewReplyList.setCurrentPage(reviewReplyList.getCurrentPage() + 1); try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(new StringReader(xml)); int eventType = xmlPullParser.getEventType(); Date replyTime = new Date(); String userName = ""; int uid = 0; // post user String content = ""; while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if ("page".equals(xmlPullParser.getName())) { reviewReplyList.setTotalPage(Integer.valueOf(xmlPullParser.getAttributeValue(null, "num"))); } else if ("item".equals(xmlPullParser.getName())) { String replyTimeStr = xmlPullParser.getAttributeValue(null, "timestamp"); replyTime = new GregorianCalendar( Integer.valueOf(replyTimeStr.substring(0, 4), 10), Integer.valueOf(replyTimeStr.substring(4, 6), 10) - 1, // start from 0 - Calendar.JANUARY Integer.valueOf(replyTimeStr.substring(6, 8), 10), Integer.valueOf(replyTimeStr.substring(8, 10), 10), Integer.valueOf(replyTimeStr.substring(10, 12), 10), Integer.valueOf(replyTimeStr.substring(12), 10) ).getTime(); } else if ("user".equals(xmlPullParser.getName())) { uid = Integer.valueOf(xmlPullParser.getAttributeValue(null, "uid")); userName = xmlPullParser.nextText(); } else if ("content".equals(xmlPullParser.getName())) { content = xmlPullParser.nextText().trim(); } break; case XmlPullParser.END_TAG: if ("item".equals(xmlPullParser.getName())) { reviewReplyList.getList().add( new ReviewReplyList.ReviewReply(replyTime, userName, uid, content)); } break; } eventType = xmlPullParser.next(); } } catch (Exception e) { e.printStackTrace(); } }
Example 15
Source File: XMLParser.java From light-novel-library_Wenku8_Android with GNU General Public License v2.0 | 4 votes |
static public NovelListWithInfo getNovelShortInfoBySearching(String xml) { try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); NovelListWithInfo n = null; xmlPullParser.setInput(new StringReader(xml)); int eventType = xmlPullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if ("metadata".equals(xmlPullParser.getName())) { n = new NovelListWithInfo(); } else if ("data".equals(xmlPullParser.getName())) { if ("Title".equals(xmlPullParser.getAttributeValue(0))) { n.name = xmlPullParser.nextText(); } else if ("TotalHitsCount".equals(xmlPullParser .getAttributeValue(0))) { n.hit = new Integer( xmlPullParser.getAttributeValue(1)); } else if ("PushCount".equals(xmlPullParser .getAttributeValue(0))) { n.push = new Integer( xmlPullParser.getAttributeValue(1)); } else if ("FavCount".equals(xmlPullParser .getAttributeValue(0))) { n.fav = new Integer( xmlPullParser.getAttributeValue(1)); } } break; } eventType = xmlPullParser.next(); } return n; } catch (Exception e) { e.printStackTrace(); return null; } }
Example 16
Source File: Wenku8Parser.java From light-novel-library_Wenku8_Android with GNU General Public License v2.0 | 4 votes |
/** * save the new xsl into an existing review list * @param reviewList the existing review list object * @param xml the fetched xml */ static public void parseReviewList(ReviewList reviewList, String xml) { reviewList.setCurrentPage(reviewList.getCurrentPage() + 1); try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(new StringReader(xml)); int eventType = xmlPullParser.getEventType(); int rid = 0; // review id Date postTime = new Date(); int noReplies = 0; Date lastReplyTime = new Date(); String userName = ""; int uid = 0; // post user String title = ""; // review title while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if ("page".equals(xmlPullParser.getName())) { reviewList.setTotalPage(Integer.valueOf(xmlPullParser.getAttributeValue(null, "num"))); } else if ("item".equals(xmlPullParser.getName())) { rid = Integer.valueOf(xmlPullParser.getAttributeValue(null, "rid")); noReplies = Integer.valueOf(xmlPullParser.getAttributeValue(null, "replies")); String postTimeStr = xmlPullParser.getAttributeValue(null, "posttime"); postTime = new GregorianCalendar( Integer.valueOf(postTimeStr.substring(0, 4), 10), Integer.valueOf(postTimeStr.substring(4, 6), 10) - 1, // start from 0 - Calendar.JANUARY Integer.valueOf(postTimeStr.substring(6, 8), 10), Integer.valueOf(postTimeStr.substring(8, 10), 10), Integer.valueOf(postTimeStr.substring(10, 12), 10), Integer.valueOf(postTimeStr.substring(12), 10) ).getTime(); String replyTimeStr = xmlPullParser.getAttributeValue(null, "replytime"); lastReplyTime = new GregorianCalendar( Integer.valueOf(replyTimeStr.substring(0, 4), 10), Integer.valueOf(replyTimeStr.substring(4, 6), 10) - 1, Integer.valueOf(replyTimeStr.substring(6, 8), 10), Integer.valueOf(replyTimeStr.substring(8, 10), 10), Integer.valueOf(replyTimeStr.substring(10, 12), 10), Integer.valueOf(replyTimeStr.substring(12), 10) ).getTime(); } else if ("user".equals(xmlPullParser.getName())) { uid = Integer.valueOf(xmlPullParser.getAttributeValue(null, "uid")); userName = xmlPullParser.nextText(); } else if ("content".equals(xmlPullParser.getName())) { title = xmlPullParser.nextText().trim(); } break; case XmlPullParser.END_TAG: if ("item".equals(xmlPullParser.getName())) { reviewList.getList().add( new ReviewList.Review(rid, postTime, noReplies, lastReplyTime, userName, uid, title)); } break; } eventType = xmlPullParser.next(); } } catch (Exception e) { e.printStackTrace(); } }
Example 17
Source File: PullRealUrlParser.java From SprintNBA with Apache License 2.0 | 4 votes |
@Override public VideoRealUrl parse(InputStream is) throws Exception { VideoRealUrl real = new VideoRealUrl(); XmlPullParser parser = Xml.newPullParser(); //由android.util.Xml创建一个XmlPullParser实例 parser.setInput(is, "UTF-8"); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if (parser.getName().equals("url")) { String urlbase = parser.nextText(); if ((urlbase.contains(".tc.qq.com")) && TextUtils.isEmpty(real.url)) { real.url = urlbase; LogUtils.i("url = " + real.url); } } else if (parser.getName().equals("fvkey")) { String vkey = parser.nextText(); LogUtils.i("vkey = " + vkey); real.fvkey = vkey; } else if (parser.getName().equals("vid")) { String vid = parser.nextText(); LogUtils.i("vid = " + vid); real.vid = vid; } else if (parser.getName().equals("fn")) { // 目前发现直接用{vid}.mp4 有部分不能播放,用fn下的可以 String fn = parser.nextText(); if (fn.endsWith(".mp4")) { LogUtils.i("fn = " + fn); real.fn = fn; } } break; case XmlPullParser.END_TAG: break; } eventType = parser.next(); } return real; }
Example 18
Source File: XMLParser.java From light-novel-library_Wenku8_Android with GNU General Public License v2.0 | 4 votes |
static public NovelFullInfo getNovelFullInfo(String xml) { try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); NovelFullInfo nfi = null; xmlPullParser.setInput(new StringReader(xml)); int eventType = xmlPullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if ("metadata".equals(xmlPullParser.getName())) { nfi = new NovelFullInfo(); } else if ("data".equals(xmlPullParser.getName())) { if ("Title".equals(xmlPullParser.getAttributeValue(0))) { nfi.aid = new Integer( xmlPullParser.getAttributeValue(1)); nfi.title = xmlPullParser.nextText(); } else if ("Author".equals(xmlPullParser .getAttributeValue(0))) { nfi.author = xmlPullParser.getAttributeValue(1); } else if ("DayHitsCount".equals(xmlPullParser .getAttributeValue(0))) { nfi.dayHitsCount = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("TotalHitsCount".equals(xmlPullParser .getAttributeValue(0))) { nfi.totalHitsCount = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("PushCount".equals(xmlPullParser .getAttributeValue(0))) { nfi.pushCount = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("FavCount".equals(xmlPullParser .getAttributeValue(0))) { nfi.favCount = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("PressId".equals(xmlPullParser .getAttributeValue(0))) { nfi.pressId = xmlPullParser.getAttributeValue(1); } else if ("BookStatus".equals(xmlPullParser .getAttributeValue(0))) { nfi.bookStatus = xmlPullParser.getAttributeValue(1); } else if ("BookLength".equals(xmlPullParser .getAttributeValue(0))) { nfi.bookLength = new Integer(xmlPullParser.getAttributeValue(1)); } else if ("LastUpdate".equals(xmlPullParser .getAttributeValue(0))) { nfi.lastUpdate = xmlPullParser.getAttributeValue(1); } else if ("LatestSection".equals(xmlPullParser .getAttributeValue(0))) { nfi.latestSectionCid = new Integer( xmlPullParser.getAttributeValue(1)); nfi.latestSectionName=xmlPullParser.nextText(); } } break; } eventType = xmlPullParser.next(); } return nfi; } catch (Exception e) { e.printStackTrace(); return null; } }
Example 19
Source File: NovelItemInfoUpdate.java From light-novel-library_Wenku8_Android with GNU General Public License v2.0 | 4 votes |
@Nullable public static NovelItemInfoUpdate parse(@NonNull String xml) { try { NovelItemInfoUpdate niiu = new NovelItemInfoUpdate(0); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(new StringReader(xml)); int eventType = xmlPullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT:// all start break; case XmlPullParser.START_TAG: if ("metadata".equals(xmlPullParser.getName())) { // Init all the value niiu.aid = 0; niiu.title = ""; niiu.author = ""; niiu.status = ""; niiu.update = ""; niiu.intro_short = ""; niiu.latest_chapter = ""; } else if ("data".equals(xmlPullParser.getName())) { if ("Title".equals(xmlPullParser.getAttributeValue(0))) { niiu.aid = Integer.valueOf( xmlPullParser.getAttributeValue(1)); niiu.title = xmlPullParser.nextText(); } else if ("Author".equals(xmlPullParser .getAttributeValue(0))) { niiu.author = xmlPullParser.getAttributeValue(1); } else if ("BookStatus".equals(xmlPullParser .getAttributeValue(0))) { niiu.status = xmlPullParser.getAttributeValue(1); } else if ("LastUpdate".equals(xmlPullParser .getAttributeValue(0))) { niiu.update = xmlPullParser.getAttributeValue(1); } else if ("IntroPreview".equals(xmlPullParser .getAttributeValue(0))) { // need to remove leading space '\u3000' niiu.intro_short = xmlPullParser.nextText().replaceAll("[ | ]", " ").trim();//.trim().replaceAll("\u3000",""); } } break; } eventType = xmlPullParser.next(); } return niiu; } catch (Exception e) { e.printStackTrace(); return null; } }
Example 20
Source File: CumulusXmlParser.java From CumulusTV with MIT License | 4 votes |
private static Channel parseChannel(XmlPullParser parser) throws IOException, XmlPullParserException, ParseException { String id = null; boolean repeatPrograms = false; for (int i = 0; i < parser.getAttributeCount(); ++i) { String attr = parser.getAttributeName(i); String value = parser.getAttributeValue(i); if (ATTR_ID.equalsIgnoreCase(attr)) { id = value; } else if (ATTR_REPEAT_PROGRAMS.equalsIgnoreCase(attr)) { repeatPrograms = "TRUE".equalsIgnoreCase(value); } } String displayName = null; String displayNumber = null; XmlTvIcon icon = null; XmlTvAppLink appLink = null; Advertisement advertisement = null; while (parser.next() != XmlPullParser.END_DOCUMENT) { if (parser.getEventType() == XmlPullParser.START_TAG) { if (TAG_DISPLAY_NAME.equalsIgnoreCase(parser.getName()) && displayName == null) { displayName = parser.nextText(); } else if (TAG_DISPLAY_NUMBER.equalsIgnoreCase(parser.getName()) && displayNumber == null) { displayNumber = parser.nextText(); } else if (TAG_ICON.equalsIgnoreCase(parser.getName()) && icon == null) { icon = parseIcon(parser); } else if (TAG_APP_LINK.equalsIgnoreCase(parser.getName()) && appLink == null) { appLink = parseAppLink(parser); } else if (TAG_AD.equalsIgnoreCase(parser.getName()) && advertisement == null) { advertisement = parseAd(parser, TAG_CHANNEL); } } else if (TAG_CHANNEL.equalsIgnoreCase(parser.getName()) && parser.getEventType() == XmlPullParser.END_TAG) { break; } } if (TextUtils.isEmpty(id) || TextUtils.isEmpty(displayName)) { throw new IllegalArgumentException("id and display-name can not be null."); } // Developers should assign original network ID in the right way not using the fake ID. InternalProviderData internalProviderData = new InternalProviderData(); internalProviderData.setRepeatable(repeatPrograms); Channel.Builder builder = new Channel.Builder() .setDisplayName(displayName) .setDisplayNumber(displayNumber) .setOriginalNetworkId(id.hashCode()) .setInternalProviderData(internalProviderData) .setTransportStreamId(0) .setServiceId(0); if (icon != null) { builder.setChannelLogo(icon.src); } if (appLink != null) { builder.setAppLinkColor(appLink.color) .setAppLinkIconUri(appLink.icon.src) .setAppLinkIntentUri(appLink.intentUri) .setAppLinkPosterArtUri(appLink.posterUri) .setAppLinkText(appLink.text); } if (advertisement != null) { List<Advertisement> advertisements = new ArrayList<>(1); advertisements.add(advertisement); internalProviderData.setAds(advertisements); builder.setInternalProviderData(internalProviderData); } return builder.build(); }