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

The following examples show how to use org.xmlpull.v1.XmlPullParserFactory#newPullParser() . 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: XmlToJson.java    From alipay-master with GNU General Public License v3.0 6 votes vote down vote up
private
@Nullable
JSONObject convertToJSONObject() {
    try {
        Tag parentTag = new Tag("", "xml");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);   // tags with namespace are taken as-is ("namespace:tagname")
        XmlPullParser xpp = factory.newPullParser();

        setInput(xpp);

        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.START_DOCUMENT) {
            eventType = xpp.next();
        }
        readTags(parentTag, xpp);

        unsetInput();

        return convertTagToJson(parentTag, false);
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
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: AtomParser.java    From PkRSS with Apache License 2.0 6 votes vote down vote up
public AtomParser() {
	// Initialize DateFormat object with the default date formatting
	dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
	dateFormat.setTimeZone(Calendar.getInstance().getTimeZone());
	pattern = Pattern.compile("-\\d{1,4}x\\d{1,4}");

	// Initialize XmlPullParser object with a common configuration
	XmlPullParser parser = null;
	try {
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		factory.setNamespaceAware(false);
		parser = factory.newPullParser();
	}
	catch (XmlPullParserException e) {
		e.printStackTrace();
	}
	xmlParser = parser;
}
 
Example 4
Source File: XMLParser.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 6 votes vote down vote up
static public int getNovelListWithInfoPageNum(String xml) {
	try {
		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:
				break;

			case XmlPullParser.START_TAG:
				if ("page".equals(xmlPullParser.getName())) {
					return new Integer(xmlPullParser.getAttributeValue(0));
				}
				break;
			}
			eventType = xmlPullParser.next();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return 0; // default
}
 
Example 5
Source File: XMLParser.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 6 votes vote down vote up
static public int getNovelListWithInfoPageNum(String xml) {
	try {
		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:
				break;

			case XmlPullParser.START_TAG:
				if ("page".equals(xmlPullParser.getName())) {
					return new Integer(xmlPullParser.getAttributeValue(0));
				}
				break;
			}
			eventType = xmlPullParser.next();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return 0; // default
}
 
Example 6
Source File: XMLTVParser.java    From ChannelSurfer with MIT License 6 votes vote down vote up
public static List<Program> parse(String in) throws XmlPullParserException, IOException {
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            Log.d(TAG, "Start parsing");
            Log.d(TAG, in.substring(0, 36));
            xpp.setInput(new StringReader(in));
            int eventType = xpp.getEventType();
            Log.d(TAG, eventType+"");
//            if(eventType == XmlPullParser.START_DOCUMENT)
//                xpp.next();
            /*
            xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            xpp.setInput(new InputStreamReader(in));*/
            /*xpp.nextTag();
            xpp.nextTag();
            */return readFeed(xpp);
        } finally {
//            in.close();
        }
    }
 
Example 7
Source File: XmlToJson.java    From XmlToJson with Apache License 2.0 6 votes vote down vote up
private
@Nullable
JSONObject convertToJSONObject() {
    try {
        Tag parentTag = new Tag("", "xml");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);   // tags with namespace are taken as-is ("namespace:tagname")
        XmlPullParser xpp = factory.newPullParser();

        setInput(xpp);

        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.START_DOCUMENT) {
            eventType = xpp.next();
        }
        readTags(parentTag, xpp);

        unsetInput();

        return convertTagToJson(parentTag, false);
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 8
Source File: XppConfigTokenizer.java    From cache2k with Apache License 2.0 5 votes vote down vote up
public XppConfigTokenizer(final String _source, final InputStream is, final String _encoding)
  throws XmlPullParserException {
  super(_source);
  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
  input = factory.newPullParser();
  input.setInput(is, _encoding);
}
 
Example 9
Source File: DeviceManager.java    From timelapse-sony with GNU General Public License v3.0 5 votes vote down vote up
private void parseCameraModels() {
    try {
        XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
        xppf.setNamespaceAware(true);
        XmlPullParser xpp = xppf.newPullParser();
        InputStream is = new FileInputStream(new File(mApplication.getFilesDir(), FILENAME));
        xpp.setInput(is, null);
        parseCameraModels(xpp);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: KmlTestUtil.java    From android-maps-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an XmlPullParser for the given KML file
 * @param fileName the file name for a KML file
 * @return an XmlPullParser for the given KML file
 * @throws XmlPullParserException, IOException
 */
static XmlPullParser createParser(String fileName) throws XmlPullParserException, IOException {
    InputStream stream = KmlTestUtil.class.getClassLoader().getResourceAsStream(fileName);
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser parser = factory.newPullParser();
    parser.setInput(stream, null);
    return parser;
}
 
Example 11
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 12
Source File: XmlParserBase.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");
	next(xpp);
	nextNoWhitespace(xpp);

	return xpp;
}
 
Example 13
Source File: XmlParserBase.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);
	factory.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, false);
	XmlPullParser xpp = factory.newPullParser();
	xpp.setInput(input, "UTF-8");
	next(xpp);
	nextNoWhitespace(xpp);

	return xpp;
}
 
Example 14
Source File: XmlParserBase.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);
	factory.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, false);
	XmlPullParser xpp = factory.newPullParser();
	xpp.setInput(input, "UTF-8");
	next(xpp);
	nextNoWhitespace(xpp);

	return xpp;
}
 
Example 15
Source File: PepXMLFileImport.java    From PDV with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Return additional parameters
 * @throws IOException
 * @throws XmlPullParserException
 */
private void getScoreName() throws IOException, XmlPullParserException {

    BufferedReader bufferedReader = new BufferedReader(new FileReader(pepXMLFile));

    checkPeptideProphet(new BufferedReader(new FileReader(pepXMLFile)));

    XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
    xmlPullParserFactory.setNamespaceAware(true);
    XmlPullParser xmlPullParser = xmlPullParserFactory.newPullParser();

    xmlPullParser.setInput(bufferedReader);

    String tagName;
    int tagNum;
    while ((tagNum = xmlPullParser.next()) != XmlPullParser.START_TAG) {
    }

    while (tagNum != XmlPullParser.END_DOCUMENT) {
        tagName = xmlPullParser.getName();

        if (tagName != null) {
            if (tagNum == XmlPullParser.START_TAG && xmlPullParser.getName().equals("search_score")) {
                String name = null;
                String value = null;
                for (int i = 0; i < xmlPullParser.getAttributeCount(); i++) {
                    String attributeName = xmlPullParser.getAttributeName(i);
                    if (attributeName.equals("name")) {
                        name = xmlPullParser.getAttributeValue(i).replaceAll("[^a-zA-Z]", "");
                        if(name.length() >= 30){
                            name = name.substring(0, 29);
                        }
                    } else if (attributeName.equals("value")) {
                        value = xmlPullParser.getAttributeValue(i);
                    }
                    if(!scoreName.contains(name)){
                        scoreName.add(name);
                    }
                }
            } else if (tagNum == XmlPullParser.END_TAG && tagName.equals("search_hit")) {
                break;
            }
        }
        tagNum = xmlPullParser.next();
    }
}
 
Example 16
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 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 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 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 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 19
Source File: Wenku8Parser.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 4 votes vote down vote up
@NonNull
static public ArrayList<VolumeList> getVolumeList(@NonNull String xml) {
    ArrayList<VolumeList> l = new ArrayList<>();
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xmlPullParser = factory.newPullParser();
        VolumeList vl = null;
        ChapterInfo ci;
        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 ("volume".equals(xmlPullParser.getName())) {
                        vl = new VolumeList();
                        vl.chapterList = new ArrayList<>();
                        vl.vid = Integer.valueOf(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 = Integer.valueOf(xmlPullParser.getAttributeValue(0));
                        ci.chapterName = xmlPullParser.nextText();
                        Log.v("MewX-XML", ci.cid + "; " + ci.chapterName);
                        if(vl != null) vl.chapterList.add(ci);
                    }
                    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;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return l;
}
 
Example 20
Source File: XppFactory.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new XmlPullParser using the XPP factory.
 * 
 * @return a new parser instance
 * @throws XmlPullParserException if the factory fails
 * @since 1.4.1
 */
public static XmlPullParser createDefaultParser() throws XmlPullParserException {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    return factory.newPullParser();
}