List of usage examples for android.util Xml newPullParser
public static XmlPullParser newPullParser()
From source file:com.none.tom.simplerssreader.opml.OPMLParser.java
public static OPMLFile parse(final InputStream in, final int mode) throws OPMLParserException, XmlPullParserException, IOException { final XmlPullParser parser = Xml.newPullParser(); try {// w w w . jav a2 s .co m parser.setInput(in, null); parser.nextTag(); final String encoding = parser.getInputEncoding(); // <opml> must contain only one attribute; "version" if (TextUtils.isEmpty(encoding) || !encoding.equalsIgnoreCase(StandardCharsets.UTF_8.name()) || parser.getAttributeCount() != 1 || TextUtils.isEmpty(validateVersion(parser, false))) { throw new OPMLParserException(parser); } return parseOPML(parser, mode); } finally { if (in != null) { in.close(); } } }
From source file:org.openaccessbutton.openaccessbutton.NavigationXmlParser.java
/** * Parse an InputStream containing the navigation items * @param in InputStream containing navigation items XML * @return List of NavigationItems/* w w w .j a v a2s. c om*/ * @throws XmlPullParserException * @throws IOException */ public List<NavigationItem> parse(InputStream in) throws XmlPullParserException, IOException { try { XmlPullParser parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); parser.setInput(in, null); parser.nextTag(); mItems = readFeed(parser); } finally { in.close(); } return mItems; }
From source file:com.connectsdk.service.airplay.PListParser.java
public JSONObject parse(InputStream in) throws XmlPullParserException, IOException, JSONException { try {/*from ww w . ja v a 2 s . c o m*/ XmlPullParser parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); parser.setInput(in, null); parser.nextTag(); return readPlist(parser); } finally { in.close(); } }
From source file:com.nextgis.mobile.forms.DescriptionFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { this.setRetainInstance(true); View view = inflater.inflate(R.layout.descriptfragment, container, false); final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item); final Map<String, ArrayList<String>> mlCategories = new HashMap<String, ArrayList<String>>(); //fill spinners from xml data XmlPullParser parser = Xml.newPullParser(); try {/*ww w .j a v a 2s. c om*/ File file = new File(getActivity().getExternalFilesDir(null), "categories.xml"); if (file != null) { if (!file.exists()) { createExternalStoragePrivateFile(); file = new File(getActivity().getExternalFilesDir(null), "categories.xml"); } InputStream in = new BufferedInputStream(new FileInputStream(file)); InputStreamReader isr = new InputStreamReader(in); // auto-detect the encoding from the stream parser.setInput(isr); int eventType = parser.getEventType(); String sCatVal = null; while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: String name = parser.getName(); if (name.equalsIgnoreCase("category")) { sCatVal = parser.getAttributeValue(null, "name"); adapter.add(sCatVal); mlCategories.put(sCatVal, new ArrayList<String>()); } else if (name.equalsIgnoreCase("subcategory")) { if (sCatVal != null) { String sSubCatVal = parser.getAttributeValue(null, "name"); mlCategories.get(sCatVal).add(sSubCatVal); } } break; } eventType = parser.next(); } if (in != null) { in.close(); } } } catch (IOException e) { // TODO } catch (Exception e) { // TODO } Spinner spinner = (Spinner) view.findViewById(R.id.spinner_cat); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { final Spinner subspinner = (Spinner) getView().findViewById(R.id.spinner_subcat); //subspinner String sCat = adapter.getItem(arg2).toString(); TextView textview = (TextView) getView().findViewById(R.id.spinner_subcat_custom); if (sCat.equalsIgnoreCase("custom")) { //enable text item textview.setEnabled(true); } else { textview.setEnabled(false); } ArrayAdapter<String> subadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, mlCategories.get(sCat)); subadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); subspinner.setAdapter(subadapter); } public void onNothingSelected(AdapterView<?> arg0) { } }); onStoreValues(); return view; }
From source file:org.redbus.arrivaltime.ArrivalTimeHelper.java
private static void getBusTimesResponse(BusDataRequest request) { if (request.content == null) { try {/*from www . jav a2s .c o m*/ request.callback.onAsyncGetBusTimesError(request.requestId, BUSSTATUS_HTTPERROR, "A network error occurred"); } catch (Throwable t) { } return; } if (request.content.toLowerCase().contains("doesn't exist")) { try { request.callback.onAsyncGetBusTimesError(request.requestId, BUSSTATUS_BADSTOPCODE, "The BusStop code was invalid"); } catch (Throwable t) { } return; } HashMap<String, ArrivalTime> wasDiverted = new HashMap<String, ArrivalTime>(); HashMap<String, ArrivalTime> hasTime = new HashMap<String, ArrivalTime>(); HashMap<String, Boolean> validServices = new HashMap<String, Boolean>(); ArrayList<ArrivalTime> allBusTimes = new ArrayList<ArrivalTime>(); ArrayList<ArrivalTime> validBusTimes = new ArrayList<ArrivalTime>(); try { XmlPullParser parser = Xml.newPullParser(); parser.setInput(new StringReader(request.content)); boolean inBusStopServiceSelector = false; boolean grabValidService = false; while (parser.next() != XmlPullParser.END_DOCUMENT) { String tagName = parser.getName(); switch (parser.getEventType()) { case XmlPullParser.START_TAG: if (tagName.equals("tr")) { String classAttr = ""; String styleAttr = parser.getAttributeValue(null, "style"); if (styleAttr != null) { if (styleAttr.contains("display") && styleAttr.contains("none")) classAttr = "BADTIME"; } else { classAttr = parser.getAttributeValue(null, "class"); if (classAttr == null) continue; if ((!classAttr.contains("tblanc")) && (!classAttr.contains("tgris"))) continue; classAttr = classAttr.replace("tblanc", ""); classAttr = classAttr.replace("tgris", ""); classAttr = classAttr.trim().toLowerCase(); } ArrivalTime bt = parseStopTime(parser, request.stopCode); bt.cssClass = classAttr; if (bt.isDiverted) { if (wasDiverted.containsKey(bt.service)) continue; wasDiverted.put(bt.service.toLowerCase(), bt); } else { hasTime.put(bt.service.toLowerCase(), bt); allBusTimes.add(bt); } } else if (tagName.equals("select")) { String idAttr = parser.getAttributeValue(null, "id"); if (idAttr == null) continue; if (!idAttr.contains("busStopService")) continue; inBusStopServiceSelector = true; } else if (tagName.equals("option")) { if (!inBusStopServiceSelector) break; grabValidService = true; } break; case XmlPullParser.TEXT: if (grabValidService) { String serviceName = parser.getText().toLowerCase(); validServices.put(serviceName.substring(0, serviceName.indexOf(' ')), new Boolean(true)); } grabValidService = false; break; case XmlPullParser.END_TAG: if (tagName.equals("select")) inBusStopServiceSelector = false; break; } } // find the "bad" css class String badCssClass = "BADTIME"; for (ArrivalTime at : allBusTimes) { if (!validServices.containsKey(at.service.toLowerCase())) { badCssClass = at.cssClass; break; } } // filter out bad times for (ArrivalTime at : allBusTimes) { if ((!validServices.containsKey(at.service.toLowerCase())) || at.cssClass.equals(badCssClass)) continue; validBusTimes.add(at); } // and add in any diverted services for (ArrivalTime at : wasDiverted.values()) { if (!validServices.containsKey(at.service.toLowerCase())) continue; if (hasTime.containsKey(at.service.toLowerCase())) continue; allBusTimes.add(at); } } catch (Throwable t) { Log.e("BusDataHelper.GetBusTimesResponse", request.content, t); try { request.callback.onAsyncGetBusTimesError(request.requestId, BUSSTATUS_BADDATA, "Invalid data was received from the bus website"); } catch (Throwable t2) { } return; } try { request.callback.onAsyncGetBusTimesSuccess(request.requestId, validBusTimes); } catch (Throwable t) { } }
From source file:com.learnit.LearnIt.async_tasks.GetHelpWordsGoogleTask.java
@Override protected List<String> doInBackground(Object... unused) { String newWord = StringUtils.stripFromArticle(_context, _word); String fullUrl = null;/*from w w w .ja v a2 s .co m*/ try { fullUrl = String.format(GOOGLE_COMPLETE_URL, URLEncoder.encode(newWord, ENCODING), Utils.getCurrentLanguages(_context).first); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } if (fullUrl == null) { return null; } Set<String> words = new TreeSet<>(); HttpUriRequest getRequest = new HttpGet(fullUrl); try { HttpResponse getResponse = client.execute(getRequest); HttpEntity resEntityGet = getResponse.getEntity(); if (resEntityGet != null) { InputStream in = resEntityGet.getContent(); try { XmlPullParser parser = Xml.newPullParser(); parser.setInput(in, null); int event = parser.getEventType(); while (event != XmlPullParser.END_DOCUMENT) { String name = parser.getName(); switch (event) { case XmlPullParser.START_TAG: break; case XmlPullParser.END_TAG: if (name.equals(SUGGESTION_TAG)) { String tempStr = parser.getAttributeValue(null, DATA_TAG); String[] strArray = tempStr.split(" "); if (strArray.length > 0) { words.add(strArray[0]); } } break; } event = parser.next(); } } catch (XmlPullParserException ex) { ex.printStackTrace(); } finally { in.close(); } } } catch (IOException ex) { Log.e(Constants.LOG_TAG, ex.getMessage()); } return new ArrayList(words); }
From source file:it.readbeyond.minstrel.librarian.FormatHandlerEPUB.java
public Publication parseFile(File file) { Publication p = super.parseFile(file); Format format = new Format(EPUB_FORMAT); pathThumbnailSourceRelativeToOPF = null; coverManifestItemID = null;// w w w .ja v a2 s. com String OPFPath = this.getOPFPath(file); if (OPFPath != null) { try { ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ); ZipEntry OPFEntry = zipFile.getEntry(OPFPath); if (OPFEntry != null) { InputStream is; // first pass: parse metadata is = zipFile.getInputStream(OPFEntry); parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(is, null); parser.nextTag(); this.parsePackage(format, 1); is.close(); // second pass: parse manifest is = zipFile.getInputStream(OPFEntry); parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(is, null); parser.nextTag(); this.parsePackage(format, 2); is.close(); // item is valid p.isValid(true); // set the cover path, relative to the EPUB container (aka ZIP) root if (pathThumbnailSourceRelativeToOPF != null) { // concatenate OPFPath parent directory and pathThumbnailSourceRelativeToOPF File tmp = new File(new File(OPFPath).getParent(), pathThumbnailSourceRelativeToOPF); // remove leading "/" format.addMetadatum("internalPathCover", tmp.getAbsolutePath().substring(1)); } } zipFile.close(); } catch (Exception e) { // invalidate item, so it will not be added to library p.isValid(false); } } p.addFormat(format); // extract cover super.extractCover(file, format, p.getID()); return p; }
From source file:com.github.naofum.gogakudroid.AsyncDownload.java
@Override protected String doInBackground(String[] koza) { File fileTmp = new File("tmp"); try {//from w w w . ja v a2 s . c o m fc = new FfmpegController(owner, fileTmp); } catch (IOException e) { e.printStackTrace(); return owner.getString(R.string.init_error); } SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(owner); String type = sharedPref.getString("type", "m4a"); isSkip = sharedPref.getBoolean("skip_file", false); progressDialog.setMax(koza.length); String url = null; for (int i = 0; i < koza.length; i++) { // file index of this week if (MainActivity.ENGLISH.containsKey(koza[i])) { url = "http://cgi2.nhk.or.jp/gogaku/st/xml/english/" + koza[i] + "/listdataflv.xml"; } else if (koza[i].contains("levelup_")) { url = "http://cgi2.nhk.or.jp/gogaku/st/xml/" + koza[i].substring("levelup_".length()) + "/levelup/listdataflv.xml"; } else { url = "http://cgi2.nhk.or.jp/gogaku/st/xml/" + koza[i] + "/kouza/listdataflv.xml"; } try { HttpGet httpGet = new HttpGet(url); DefaultHttpClient httpClient = new DefaultHttpClient(); httpGet.setHeader("Connection", "Keep-Alive"); HttpResponse response = httpClient.execute(httpGet); int status = response.getStatusLine().getStatusCode(); if (status != HttpStatus.SC_OK) { throw new Exception(""); } else { receiveStr = EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); return owner.getString(R.string.conn_error); } XmlPullParser xmlPullParser = Xml.newPullParser(); try { xmlPullParser.setInput(new StringReader(receiveStr)); } catch (XmlPullParserException e) { Log.d(TAG, e.toString()); return owner.getString(R.string.parse_error); } try { String kouza; String hdate; String file; String nendo; int eventType; eventType = xmlPullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { if (xmlPullParser.getName().equals("music")) { kouza = xmlPullParser.getAttributeValue(null, "kouza"); hdate = xmlPullParser.getAttributeValue(null, "hdate"); file = xmlPullParser.getAttributeValue(null, "file"); nendo = xmlPullParser.getAttributeValue(null, "nendo"); lastKouza = kouza; download(koza[i], kouza, hdate, file, nendo, type); publishProgress(i); } } eventType = xmlPullParser.next(); } } catch (Exception e) { Log.d(TAG, e.toString()); return owner.getString(R.string.parse_error); } } return lastMessage; }
From source file:org.xwiki.android.authenticator.rest.XWikiConnector.java
public static XmlPullParser getXML(String url, String user, String pass) throws IOException, XmlPullParserException { HttpResponse response = executeGet(url, user, pass); int error = response.getStatusLine().getStatusCode(); if (error != HttpStatus.SC_OK) { throw new IOException("Error getting [" + url + "] with error code [" + error + ", " + response.getStatusLine() + "]"); }//from w ww . j av a 2s . c om XmlPullParser parser = Xml.newPullParser(); parser.setInput(response.getEntity().getContent(), null); return parser; }
From source file:biz.varkon.shelvesom.server.BGGInfo.java
/** * Parses a valid XML response from the specified input stream. This method * must invoke parse//from w w w . j ava2 s. c om * {@link ResponseParser#parseResponse(org.xmlpull.v1.XmlPullParser)} if the * XML response is valid, or throw an exception if it is not. * * @param in * The input stream containing the response sent by the web * service. * @param responseParser * The parser to use when the response is valid. * * @throws java.io.IOException */ public static void parseResponse(InputStream in, ResponseParser responseParser, IOUtilities.inputTypes inputType) throws IOException { final XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(new InputStreamReader(in)); int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty } /* * if (type != XmlPullParser.START_TAG) { throw new * InflateException(parser.getPositionDescription() + * ": No start tag found!"); } * * String name; boolean valid = false; final int topDepth = * parser.getDepth(); * * while (((type = parser.next()) != XmlPullParser.END_TAG || parser * .getDepth() > topDepth) && type != XmlPullParser.END_DOCUMENT) { * * if (type != XmlPullParser.START_TAG) { continue; } * * name = parser.getName(); valid = true; if * (RESPONSE_TAG_BOARDGAME.equals(name)) { valid = true; break; } } * * if (valid) */ responseParser.parseResponse(parser); } catch (XmlPullParserException e) { final IOException ioe = new IOException("Could not parse the response"); ioe.initCause(e); throw ioe; } }