Java Code Examples for org.xmlpull.v1.XmlPullParserFactory#newInstance()

The following examples show how to use org.xmlpull.v1.XmlPullParserFactory#newInstance() . 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: Rss2Parser.java    From PkRSS with Apache License 2.0 6 votes vote down vote up
/**
 * Pulls an image URL from an encoded String.
 *
 * @param encoded The String which to extract an image URL from.
 * @return The first image URL found on the encoded String. May return an
 * empty String if none were found.
 */
private String pullImageLink(String encoded) {
	try {
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser xpp = factory.newPullParser();

		xpp.setInput(new StringReader(encoded));
		int eventType = xpp.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			if (eventType == XmlPullParser.START_TAG && "img".equals(xpp.getName())) {
				int count = xpp.getAttributeCount();
				for (int x = 0; x < count; x++) {
					if (xpp.getAttributeName(x).equalsIgnoreCase("src"))
						return pattern.matcher(xpp.getAttributeValue(x)).replaceAll("");
				}
			}
			eventType = xpp.next();
		}
	}
	catch (Exception e) {
		log(TAG, "Error pulling image link from description!\n" + e.getMessage(), Log.WARN);
	}

	return "";
}
 
Example 2
Source File: GhostLuckyMoney.java    From luckymoney with Apache License 2.0 6 votes vote down vote up
private String readxml(String xmlstr) {
    int i = xmlstr.indexOf("<msg>");
    String xml = xmlstr.substring(i);
    String keyurl = "";
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader(xml));
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("nativeurl")) {
                    xpp.nextToken();
                    keyurl = xpp.getText();
                    break;
                }
            }
            eventType = xpp.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return keyurl;
}
 
Example 3
Source File: XMLParser.java    From QPM with Apache License 2.0 5 votes vote down vote up
/**
 * 通用解析XML方法,可以解析所有的xml结构,需要在方法调用之前正确设置{@link #register(String)}
 *
 * @param context
 * @param xmlParser 在外界注册好后传进来
 * @param in        xml的输入流
 * @return 返回已经解析完的结果
 * @throws XmlPullParserException
 * @throws IOException
 */
public static synchronized XMLParser parse(Context context, XMLParser xmlParser, InputStream in)
        throws XmlPullParserException, IOException {
    if (context == null || xmlParser == null || in == null) {
        return null;
    }

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    XmlPullParser parser = factory.newPullParser();
    parser.setInput(in, ENCODING);
    return parse(context, xmlParser, parser);
}
 
Example 4
Source File: SsManifestParser.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public SsManifestParser() {
  try {
    xmlParserFactory = XmlPullParserFactory.newInstance();
  } catch (XmlPullParserException e) {
    throw new RuntimeException("Couldn't create XmlPullParserFactory instance", e);
  }
}
 
Example 5
Source File: AmazonInfoRetriever.java    From BarcodeEye with Apache License 2.0 5 votes vote down vote up
private static XmlPullParser buildParser(CharSequence contents) throws XmlPullParserException {
  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
  factory.setNamespaceAware(true);
  XmlPullParser xpp = factory.newPullParser();
  xpp.setInput(new StringReader(contents.toString()));
  return xpp;
}
 
Example 6
Source File: ToolsHelper.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
protected XmlPullParser loadXml(InputStream stream) throws XmlPullParserException, IOException {
BufferedInputStream input = new BufferedInputStream(stream);
   XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
   factory.setNamespaceAware(true);
   XmlPullParser xpp = factory.newPullParser();
   xpp.setInput(input, "UTF-8");
   xpp.next();
   return xpp;
 }
 
Example 7
Source File: TtmlDecoder.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public TtmlDecoder() {
  super("TtmlDecoder");
  try {
    xmlParserFactory = XmlPullParserFactory.newInstance();
    xmlParserFactory.setNamespaceAware(true);
  } catch (XmlPullParserException e) {
    throw new RuntimeException("Couldn't create XmlPullParserFactory instance", e);
  }
}
 
Example 8
Source File: SsManifestParser.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public SsManifestParser() {
  try {
    xmlParserFactory = XmlPullParserFactory.newInstance();
  } catch (XmlPullParserException e) {
    throw new RuntimeException("Couldn't create XmlPullParserFactory instance", e);
  }
}
 
Example 9
Source File: XMLParser.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 4 votes vote down vote up
static public ArrayList<NovelListWithInfo> getNovelListWithInfo(String xml) {
	try {
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser xmlPullParser = factory.newPullParser();
		ArrayList<NovelListWithInfo> l = null;
		NovelListWithInfo n = null;
		xmlPullParser.setInput(new StringReader(xml));
		int eventType = xmlPullParser.getEventType();

		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				l = new ArrayList<NovelListWithInfo>();
				break;

			case XmlPullParser.START_TAG:

				if ("item".equals(xmlPullParser.getName())) {
					n = new NovelListWithInfo();
					n.aid = new Integer(xmlPullParser.getAttributeValue(0));
					// Log.v("MewX-XML", "aid=" + n.aid);
				} else if ("data".equals(xmlPullParser.getName())) {
					if ("Title".equals(xmlPullParser.getAttributeValue(0))) {
						n.name = xmlPullParser.nextText();
						// Log.v("MewX-XML", n.name);
					} else if ("TotalHitsCount".equals(xmlPullParser
							.getAttributeValue(0))) {
						n.hit = new Integer(
								xmlPullParser.getAttributeValue(1));
						// Log.v("MewX-XML", "hit=" + n.hit);
					} else if ("PushCount".equals(xmlPullParser
							.getAttributeValue(0))) {
						n.push = new Integer(
								xmlPullParser.getAttributeValue(1));
						// Log.v("MewX-XML", "push=" + n.push);
					} else if ("FavCount".equals(xmlPullParser
							.getAttributeValue(0))) {
						n.fav = new Integer(
								xmlPullParser.getAttributeValue(1));
						// Log.v("MewX-XML", "fav=" + n.fav);
					}
				}
				break;

			case XmlPullParser.END_TAG:
				if ("item".equals(xmlPullParser.getName())) {
					Log.v("MewX-XML", n.aid + ";" + n.name + ";" + n.hit
							+ ";" + n.push + ";" + n.fav);
					l.add(n);
					n = null;
				}
				break;
			}
			eventType = xmlPullParser.next();
		}
		return l;
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 10
Source File: XMLParser.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 4 votes vote down vote up
static public NovelIntro getNovelIntro(String xml) {
	try {
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser xmlPullParser = factory.newPullParser();
		NovelIntro ni = null;
		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())) {
					ni = new NovelIntro();
					// Log.v("MewX-XML", "aid=" + n.aid);
				} else if ("data".equals(xmlPullParser.getName())) {
					if ("Title".equals(xmlPullParser.getAttributeValue(0))) {
						ni.aid = new Integer(
								xmlPullParser.getAttributeValue(1));
						ni.title = xmlPullParser.nextText();
					} else if ("Author".equals(xmlPullParser
							.getAttributeValue(0))) {
						ni.author = xmlPullParser.getAttributeValue(1);
					} else if ("BookStatus".equals(xmlPullParser
							.getAttributeValue(0))) {
						ni.status = new Integer(
								xmlPullParser.getAttributeValue(1));
						// Log.v("MewX-XML", "push=" + n.push);
					} else if ("LastUpdate".equals(xmlPullParser
							.getAttributeValue(0))) {
						ni.update = xmlPullParser.getAttributeValue(1);
						// Log.v("MewX-XML", "fav=" + n.fav);
					} else if ("IntroPreview".equals(xmlPullParser
							.getAttributeValue(0))) {
						ni.intro_short = xmlPullParser.nextText();
						// Log.v("MewX-XML", "fav=" + n.fav);
					}
				}
				break;

			case XmlPullParser.END_TAG:
				if ("metadata".equals(xmlPullParser.getName())) {
					// nothing
				}
				break;
			}
			eventType = xmlPullParser.next();
		}
		return ni;
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 11
Source File: SLDHandler.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/** Helper method for finding which style handler/version to use from the actual content. */
Object[] getVersionAndReader(Object input) throws IOException {
    // need to determine version of sld from actual content
    BufferedReader reader = null;

    if (input instanceof InputStream) {
        reader = RequestUtils.getBufferedXMLReader((InputStream) input, XML_LOOKAHEAD);
    } else {
        reader = RequestUtils.getBufferedXMLReader(toReader(input), XML_LOOKAHEAD);
    }

    String version;
    try {
        // create stream parser
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);

        // parse root element
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(reader);
        parser.nextTag();

        version = null;
        for (int i = 0; i < parser.getAttributeCount(); i++) {
            if ("version".equals(parser.getAttributeName(i))) {
                version = parser.getAttributeValue(i);
            }
        }

        parser.setInput(null);
    } catch (XmlPullParserException e) {
        throw (IOException) new IOException("Error parsing content").initCause(e);
    }

    // reset input stream
    reader.reset();

    if (version == null) {
        LOGGER.warning("Could not determine SLD version from content. Assuming 1.0.0");
        version = "1.0.0";
    }

    return new Object[] {new Version(version), reader};
}
 
Example 12
Source File: XMLRipper.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
Map<String, String> parseXMLString(String xmlString) throws XmlPullParserException, IOException
{
    TagType baseTag = null;  // identifies what kind of document is being parsed
    String name = null;
    String uuid = null;
    String format = null;

    List<TagType> tagStack = new ArrayList<>();

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();

    xpp.setInput(new StringReader(xmlString));
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT)
    {
        TagType currentTag = TagType.map(xpp.getName());

        if (eventType == XmlPullParser.START_DOCUMENT)
        {
            // Do nothing
        }
        else if (eventType == XmlPullParser.START_TAG)
        {
            tagStack.add(currentTag);

            if (currentTag == TagType.Characteristic || currentTag == TagType.Descriptor || currentTag == TagType.Attribute || currentTag == TagType.Service)
            {
                // Parse attribute values
                name = xpp.getAttributeValue(null, "name");
                uuid = xpp.getAttributeValue(null, "uuid");
            }

            // TODO: Parse more info depending on tag?

            //System.out.println("Start tag " + xpp.getName());
        }
        else if (eventType == XmlPullParser.END_TAG)
        {
            //TODO:  Assert that the last tag in the stack matches our current tag
            tagStack.remove(tagStack.size() - 1);
            // System.out.println("End tag " + xpp.getName());
        }
        else if (eventType == XmlPullParser.TEXT)
        {
            // Inspect top of stack
            currentTag = tagStack.get(tagStack.size() - 1);

            switch (currentTag)
            {
                case Format:
                    format = xpp.getText();
            }
            // System.out.println("Text " + xpp.getText());
        }
        else
        {
            //System.out.println("Something else " + xpp.getName());
        }
        eventType = xpp.next();
    }

    System.out.println("++-- Extracted info: name=" + name + ", uuid=" + uuid + ", format=" + format);

    Map<String, String> m = new HashMap<>();
    m.put("name", name);
    m.put("uuid", uuid);
    m.put("format", format);
    // TODO: Add more attributes to map here?

    return m;
}
 
Example 13
Source File: CustomMainActivity.java    From WhereYouGo with GNU General Public License v3.0 4 votes vote down vote up
public static String getNewsFromTo(int lastVersion, int actualVersion) {
    // Logger.d(TAG, "getNewsFromTo(" + lastVersion + ", " + actualVersion + "), file:" +
    // "news_" + (Const.isPro() ? "pro" : "free") + ".xml");
    String versionInfo =
            "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>";
    String data = CustomMainActivity.loadAssetString("news.xml");
    if (data == null || data.length() == 0)
        data = CustomMainActivity.loadAssetString("news.xml");
    if (data != null && data.length() > 0) {
        XmlPullParser parser;
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            parser = factory.newPullParser();
            parser.setInput(new StringReader(data));

            int event;
            String tagName;

            boolean correct = false;
            while (true) {
                event = parser.nextToken();
                if (event == XmlPullParser.START_TAG) {
                    tagName = parser.getName();
                    if (tagName.equalsIgnoreCase("update")) {
                        String name = parser.getAttributeValue(null, "name");
                        int id = Utils.parseInt(parser.getAttributeValue(null, "id"));
                        if (id > lastVersion && id <= actualVersion) {
                            correct = true;
                            versionInfo += ("<h4>" + name + "</h4><ul>");
                        } else {
                            correct = false;
                        }
                    } else if (tagName.equalsIgnoreCase("li")) {
                        if (correct) {
                            versionInfo += ("<li>" + parser.nextText() + "</li>");
                        }
                    }
                } else if (event == XmlPullParser.END_TAG) {
                    tagName = parser.getName();
                    if (tagName.equalsIgnoreCase("update")) {
                        if (correct) {
                            correct = false;
                            versionInfo += "</ul>";
                        }
                    } else if (tagName.equals("document")) {
                        break;
                    }
                }
            }
        } catch (Exception e) {
            Logger.e(TAG, "getNews()", e);
        }
    }

    versionInfo += "</body></html>";
    return versionInfo;
}
 
Example 14
Source File: DBHelper.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Parse {@code additional_repos.xml} into a list of items. Walk through
 * all TEXT pieces of the xml file and put them into a single list of repo
 * elements.  Each repo is defined as eight elements in that list.
 * {@code additional_repos.xml} has seven elements per repo because it is
 * not allowed to set the priority since that would give it the power to
 * override {@code default_repos.xml}.
 */
public static List<String> parseAdditionalReposXml(File additionalReposFile)
        throws IOException, XmlPullParserException {
    List<String> repoItems = new LinkedList<>();
    InputStream xmlInputStream = new FileInputStream(additionalReposFile);
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser parser = factory.newPullParser();
    parser.setInput(xmlInputStream, "UTF-8");

    int eventType = parser.getEventType();
    boolean isItem = false;
    while (eventType != XmlPullParser.END_DOCUMENT) {
        String tagname = parser.getName();
        switch (eventType) {
            case XmlPullParser.START_TAG:
                if ("item".equals(tagname)) {
                    isItem = true;
                }
                break;
            case XmlPullParser.END_TAG:
                isItem = false;
                break;
            case XmlPullParser.TEXT:
                if (isItem) {
                    repoItems.add(parser.getText());
                }
                break;
        }
        eventType = parser.next();
    }
    xmlInputStream.close();

    final int priorityIndex = 5;
    for (int i = priorityIndex; i < repoItems.size(); i += REPO_XML_ITEM_COUNT) {
        repoItems.add(i, "0");
    }

    if (repoItems.size() % REPO_XML_ITEM_COUNT == 0) {
        return repoItems;
    }

    Log.e(TAG, "Ignoring " + additionalReposFile + ", wrong number of items: "
            + repoItems.size() + " % " + (REPO_XML_ITEM_COUNT - 1) + " != 0");
    return new LinkedList<>();
}
 
Example 15
Source File: JPGameGrabTask.java    From NintendoSwitchEShopHelper with GNU General Public License v3.0 4 votes vote down vote up
private void parseXMLWithPullAndAddToDb(String xmlData) {
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xmlPullParser = factory.newPullParser();
        xmlPullParser.setInput(new StringReader(xmlData));
        int eventType = xmlPullParser.getEventType();

        JPGame jpGame = null;

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String nodeName = xmlPullParser.getName();
            switch (eventType) {
                case XmlPullParser.START_TAG: {
                    switch (nodeName) {
                        case "InitialCode":
                            jpGame = new JPGame();
                            jpGame.setInitialCode(parseGameCode(xmlPullParser.nextText()));
                            break;
                        case "TitleName":
                            jpGame.setTitleName(xmlPullParser.nextText());
                            break;
                        case "MakerName":
                            jpGame.setMakerName(xmlPullParser.nextText());
                            break;
                        case "MakerKana":
                            jpGame.setMakerKana(xmlPullParser.nextText());
                            break;
                        case "Price":
                            jpGame.setPrice(xmlPullParser.nextText());
                            break;
                        case "SalesDate":
                            jpGame.setSalesDate(xmlPullParser.nextText());
                            break;
                        case "SoftType":
                            jpGame.setSoftType(xmlPullParser.nextText());
                            break;
                        case "PlatformID":
                            jpGame.setPlatformID(xmlPullParser.nextText());
                            break;
                        case "DlIconFlg":
                            jpGame.setDlIconFlg(xmlPullParser.nextText());
                            break;
                        case "LinkURL":
                            // Parse the LinkURL to complete url
                            jpGame.setLinkURL("https://ec.nintendo.com/JP/ja" + xmlPullParser.nextText());
                            break;
                        case "ScreenshotImgFlg":
                            jpGame.setScreenshotImgFlg(xmlPullParser.nextText());
                            break;
                        case "ScreenshotImgURL":
                            jpGame.setScreenshotImgURL(xmlPullParser.nextText());
                            jpGame.setNsUid(parseNsUid(jpGame.getLinkURL()));
                            addJPGame(jpGame);
                            break;
                    }
                    break;
                }
                case XmlPullParser.END_TAG: {
                    break;
                }
                default:
                    break;
            }
            eventType = xmlPullParser.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 16
Source File: Wenku8Parser.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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: XMLParser.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 4 votes vote down vote up
static public ArrayList<VolumeList> getVolumeList(String xml) {
	try {
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser xmlPullParser = factory.newPullParser();
		ArrayList<VolumeList> l = null;
		VolumeList vl = null;
		ChapterInfo ci = null;
		xmlPullParser.setInput(new StringReader(xml));
		int eventType = xmlPullParser.getEventType();

		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				l = new ArrayList<VolumeList>();
				break;

			case XmlPullParser.START_TAG:

				if ("volume".equals(xmlPullParser.getName())) {
					vl = new VolumeList();
					vl.chapterList = new ArrayList<ChapterInfo>();
					vl.vid = new Integer(xmlPullParser.getAttributeValue(0));

					// Here the returned text has some format error
					// And I will handle them then
					Log.v("MewX-XML", "+ " + vl.vid + "; ");
				} else if ("chapter".equals(xmlPullParser.getName())) {
					ci = new ChapterInfo();
					ci.cid = new Integer(xmlPullParser.getAttributeValue(0));
					ci.chapterName = xmlPullParser.nextText();
					Log.v("MewX-XML", ci.cid + "; " + ci.chapterName);
					vl.chapterList.add(ci);
					ci = null;
				}
				break;

			case XmlPullParser.END_TAG:
				if ("volume".equals(xmlPullParser.getName())) {
					l.add(vl);
					vl = null;
				}
				break;
			}
			eventType = xmlPullParser.next();
		}

		/** Handle the rest problem */
		// Problem like this:
		// <volume vid="41748"><![CDATA[第一卷 告白于苍刻之夜]]>
		// <chapter cid="41749"><![CDATA[序章]]></chapter>
		int currentIndex = 0;
		for (int i = 0; i < l.size(); i++) {
			currentIndex = xml.indexOf("volume", currentIndex);
			if (currentIndex != -1) {
				currentIndex = xml.indexOf("CDATA[", currentIndex);
				if (xml.indexOf("volume", currentIndex) != -1) {
					int beg = currentIndex + 6;
					int end = xml.indexOf("]]", currentIndex);

					if (end != -1) {
						l.get(i).volumeName = xml.substring(beg, end);
						Log.v("MewX-XML", "+ " + l.get(i).volumeName + "; ");
						currentIndex = end + 1;
					} else
						break;

				} else
					break;
			} else
				break;
		}

		return l;
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 18
Source File: XMLParser.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 4 votes vote down vote up
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: XMLParser.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 4 votes vote down vote up
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 20
Source File: RPCConstructorsTests.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Map<String, List<Parameter>> getRPCMandatoryParamsMap(String fileName) {
    Map<String, List<Parameter>> rpcMandatoryParamsMap = new HashMap<>();
    try {
        InputStream stream = this.mContext.getAssets().open(fileName);
        XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
        XmlPullParser myParser = xmlFactoryObject.newPullParser();
        myParser.setInput(stream, null);
        int event = myParser.getEventType();
        String rpcName = null;
        boolean ignoreRPC = false;
        while (event != XmlPullParser.END_DOCUMENT)  {
            String name = myParser.getName();
            switch (event){
                case XmlPullParser.START_TAG:
                    // Store the RPC name in the map
                    if(name.equals("function") || name.equals("struct")){
                        rpcName = myParser.getAttributeValue(null,"name");
                        ignoreRPC = false;
                        if (name.equals("function") && myParser.getAttributeValue(null, "messagetype").equals("response") && !rpcName.contains("Response")){
                            rpcName += "Response";
                        }

                        // -------------- Exceptional cases because of mismatch between the RPC spec and the Android code --------------
                            if(rpcName.equals("SyncMsgVersion")){
                                rpcName = "SdlMsgVersion";
                            } else if(rpcName.equals("ShowConstantTBTResponse")){
                                rpcName = "ShowConstantTbtResponse";
                            } else if(rpcName.equals("OASISAddress")) {
                                rpcName = "OasisAddress";
                            } else if(rpcName.equals("ShowConstantTBT")) {
                                rpcName = "ShowConstantTbt";
                            } else if (rpcName.equals("EncodedSyncPData") || rpcName.equals("OnEncodedSyncPData") || rpcName.equals("EncodedSyncPDataResponse")){
                                ignoreRPC = true;
                            }
                        // -------------------------------------------------------------------------------------------------------------

                        if (!ignoreRPC) {
                            rpcMandatoryParamsMap.put(rpcName, new ArrayList<Parameter>());
                        }
                    }
                    // Store the mandatory params for the current RPC in the map
                    if(name.equals("param") && myParser.getAttributeValue(null, "until") == null && !ignoreRPC){
                        boolean mandatory = Boolean.valueOf(myParser.getAttributeValue(null,"mandatory"));
                        if (mandatory) {
                            String paramName = myParser.getAttributeValue(null, "name");
                            String paramType = myParser.getAttributeValue(null, "type");
                            boolean paramIsArray = Boolean.valueOf(myParser.getAttributeValue(null, "array"));

                            // -------------- Exceptional cases because of mismatch between the RPC spec and the Android code --------------
                            if (paramType.equals("SyncMsgVersion")){
                                paramType = "SdlMsgVersion";
                            } else if (rpcName.equals("GPSData") && paramType.equals("Float")){
                                paramType = "Double";
                            } else if (rpcName.equals("TouchEvent") && paramType.equals("Integer") && paramIsArray){
                                paramType = "Long";
                            }

                            if (paramName.equals("syncFileName")){
                                paramName = "sdlFileName";
                            } else if (paramName.equals("syncMsgVersion")){
                                paramName = "sdlMsgVersion";
                            } else if (paramName.equals("hmiPermissions")){
                                paramName = "hMIPermissions";
                            } else if (paramName.equals("resolution")){
                                paramName = "imageResolution";
                            } else if (paramName.equals("pressureTelltale")){
                                paramName = "pressureTellTale";
                            }
                            // -------------------------------------------------------------------------------------------------------------

                            Parameter param = new Parameter(paramName, paramType, paramIsArray);
                            rpcMandatoryParamsMap.get(rpcName).add(param);
                        }
                    }
                    break;
            }
            event = myParser.next();
        }
        stream.close();
    } catch (IOException | XmlPullParserException e) {
        e.printStackTrace();
    }
    return rpcMandatoryParamsMap;
}