Java Code Examples for org.xmlpull.v1.XmlPullParser#START_DOCUMENT

The following examples show how to use org.xmlpull.v1.XmlPullParser#START_DOCUMENT . 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 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 2
Source File: PluginManifestUtil.java    From AndroidPlugin with MIT License 5 votes vote down vote up
private static void setAttrs(PlugInfo info, String manifestXML)
        throws XmlPullParserException, IOException {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser parser = factory.newPullParser();
    parser.setInput(new StringReader(manifestXML));
    int eventType = parser.getEventType();
    String namespaceAndroid = null;
    do {
        switch (eventType) {
        case XmlPullParser.START_DOCUMENT: {
            break;
        }
        case XmlPullParser.START_TAG: {
            String tag = parser.getName();
            if (tag.equals("manifest")) {
                namespaceAndroid = parser.getNamespace("android");
            } else if ("activity".equals(parser.getName())) {
                addActivity(info, namespaceAndroid, parser);
            } else if ("receiver".equals(parser.getName())) {
                addReceiver(info, namespaceAndroid, parser);
            } else if ("service".equals(parser.getName())) {
                addService(info, namespaceAndroid, parser);
            } else if ("application".equals(parser.getName())) {
                parseApplicationInfo(info, namespaceAndroid, parser);
            }
            break;
        }
        case XmlPullParser.END_TAG: {
            break;
        }
        }
        eventType = parser.next();
    } while (eventType != XmlPullParser.END_DOCUMENT);
}
 
Example 3
Source File: WeixinUtil.java    From android-common-utils with Apache License 2.0 5 votes vote down vote up
public static Map<String,String> decodeXml(String content) {
    try {
        Map<String, String> xml = new HashMap<>();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(new StringReader(content));
        int event = parser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {

            String nodeName=parser.getName();
            switch (event) {
                case XmlPullParser.START_DOCUMENT:

                    break;
                case XmlPullParser.START_TAG:
                    if(!"xml".equals(nodeName)){
                        //实例化student对象
                        xml.put(nodeName,parser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    break;
            }
            event = parser.next();
        }
        return xml;
    } catch (Exception e) {
        Log.e("orion",e.toString());
    }
    return null;

}
 
Example 4
Source File: GradleDependencyEntity.java    From freeline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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 5
Source File: GattInfo.java    From SensorTag-CC2650 with Apache License 2.0 5 votes vote down vote up
private void readUuidData(XmlResourceParser xpp) throws XmlPullParserException, IOException {
  xpp.next();
  String tagName = null;
  String uuid = null;
  String descr = null;
  String icon = null;
  int eventType = xpp.getEventType(); 

  while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_DOCUMENT) {
      // do nothing
    } else if (eventType == XmlPullParser.START_TAG) {
      tagName = xpp.getName();
      uuid = xpp.getAttributeValue(null, "uuid");
      descr = xpp.getAttributeValue(null, "descr");
      icon = xpp.getAttributeValue(null,"icon");
    } else if (eventType == XmlPullParser.END_TAG) {
      // do nothing
    } else if (eventType == XmlPullParser.TEXT) {
      if (tagName.equalsIgnoreCase("item")) {
        if (!uuid.isEmpty()) {
          uuid = uuid.replace("0x", "");
          mNameMap.put(uuid, xpp.getText());
          mDescrMap.put(uuid, descr);
          mIconMap.put(uuid, icon);
        }
      }
    }
    eventType = xpp.next();
  }
}
 
Example 6
Source File: AbstractGrammar.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void executeAction( Dsmlv2Container container ) throws XmlPullParserException, IOException
{
    XmlPullParser xpp = container.getParser();

    int eventType = xpp.getEventType();

    do
    {
        switch ( eventType )
        {
            case XmlPullParser.START_DOCUMENT:
                container.setState( Dsmlv2StatesEnum.INIT_GRAMMAR_STATE );
                break;

            case XmlPullParser.END_DOCUMENT:
                container.setState( Dsmlv2StatesEnum.GRAMMAR_END );
                break;

            case XmlPullParser.START_TAG:
                processTag( container, Tag.START );
                break;

            case XmlPullParser.END_TAG:
                processTag( container, Tag.END );
                break;

            default:
                break;
        }

        eventType = xpp.next();
    }
    while ( eventType != XmlPullParser.END_DOCUMENT );
}
 
Example 7
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 8
Source File: DashManifestParser.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Parses an event object.
 *
 * @param xpp The current xml parser.
 * @param scratchOutputStream A {@link ByteArrayOutputStream} that's used when parsing the object.
 * @return The serialized byte array.
 * @throws XmlPullParserException If there is any error parsing this node.
 * @throws IOException If there is any error reading from the underlying input stream.
 */
protected byte[] parseEventObject(XmlPullParser xpp, ByteArrayOutputStream scratchOutputStream)
    throws XmlPullParserException, IOException {
  scratchOutputStream.reset();
  XmlSerializer xmlSerializer = Xml.newSerializer();
  xmlSerializer.setOutput(scratchOutputStream, C.UTF8_NAME);
  // Start reading everything between <Event> and </Event>, and serialize them into an Xml
  // byte array.
  xpp.nextToken();
  while (!XmlPullParserUtil.isEndTag(xpp, "Event")) {
    switch (xpp.getEventType()) {
      case (XmlPullParser.START_DOCUMENT):
        xmlSerializer.startDocument(null, false);
        break;
      case (XmlPullParser.END_DOCUMENT):
        xmlSerializer.endDocument();
        break;
      case (XmlPullParser.START_TAG):
        xmlSerializer.startTag(xpp.getNamespace(), xpp.getName());
        for (int i = 0; i < xpp.getAttributeCount(); i++) {
          xmlSerializer.attribute(xpp.getAttributeNamespace(i), xpp.getAttributeName(i),
              xpp.getAttributeValue(i));
        }
        break;
      case (XmlPullParser.END_TAG):
        xmlSerializer.endTag(xpp.getNamespace(), xpp.getName());
        break;
      case (XmlPullParser.TEXT):
        xmlSerializer.text(xpp.getText());
        break;
      case (XmlPullParser.CDSECT):
        xmlSerializer.cdsect(xpp.getText());
        break;
      case (XmlPullParser.ENTITY_REF):
        xmlSerializer.entityRef(xpp.getText());
        break;
      case (XmlPullParser.IGNORABLE_WHITESPACE):
        xmlSerializer.ignorableWhitespace(xpp.getText());
        break;
      case (XmlPullParser.PROCESSING_INSTRUCTION):
        xmlSerializer.processingInstruction(xpp.getText());
        break;
      case (XmlPullParser.COMMENT):
        xmlSerializer.comment(xpp.getText());
        break;
      case (XmlPullParser.DOCDECL):
        xmlSerializer.docdecl(xpp.getText());
        break;
      default: // fall out
    }
    xpp.nextToken();
  }
  xmlSerializer.flush();
  return scratchOutputStream.toByteArray();
}
 
Example 9
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 10
Source File: XmlManifestReader.java    From AndroidPlugin with MIT License 4 votes vote down vote up
public static String getManifestXMLFromAPK(ZipFile file, ZipEntry entry) {
    StringBuilder xmlSb = new StringBuilder(100);
    XmlResourceParser parser = null;
    try {
        parser = new XmlResourceParser();
        parser.open(file.getInputStream(entry));

        StringBuilder sb = new StringBuilder(10);
        final String indentStep = "	";

        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            switch (type) {
            case XmlPullParser.START_DOCUMENT: {
                log(xmlSb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                break;
            }
            case XmlPullParser.START_TAG: {
                log(false, xmlSb, "%s<%s%s", sb,
                        getNamespacePrefix(parser.getPrefix()),
                        parser.getName());
                sb.append(indentStep);

                int namespaceCountBefore = parser.getNamespaceCount(parser
                        .getDepth() - 1);
                int namespaceCount = parser.getNamespaceCount(parser
                        .getDepth());

                for (int i = namespaceCountBefore; i != namespaceCount; ++i) {
                    log(xmlSb, "%sxmlns:%s=\"%s\"",
                            i == namespaceCountBefore ? "  " : sb,
                            parser.getNamespacePrefix(i),
                            parser.getNamespaceUri(i));
                }

                for (int i = 0, size = parser.getAttributeCount(); i != size; ++i) {
                    log(false,
                            xmlSb,
                            "%s%s%s=\"%s\"",
                            " ",
                            getNamespacePrefix(parser.getAttributePrefix(i)),
                            parser.getAttributeName(i),
                            getAttributeValue(parser, i));
                }
                // log("%s>",sb);
                log(xmlSb, ">");
                break;
            }
            case XmlPullParser.END_TAG: {
                sb.setLength(sb.length() - indentStep.length());
                log(xmlSb, "%s</%s%s>", sb,
                        getNamespacePrefix(parser.getPrefix()),
                        parser.getName());
                break;
            }
            case XmlPullParser.TEXT: {
                log(xmlSb, "%s%s", sb, parser.getText());
                break;
            }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        parser.close();
    }
    return xmlSb.toString();
}
 
Example 11
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 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 12
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 13
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 14
Source File: ReadRssFeed.java    From codeexamples-android with Eclipse Public License 1.0 4 votes vote down vote up
public void parse(String rssFeed) {
	XmlPullParser parser = Xml.newPullParser();
	try {
		// auto-detect the encoding from the stream
		parser.setInput(new URL(rssFeed).openConnection().getInputStream(),
				null);
		int eventType = parser.getEventType();
		boolean done = false;
		while (eventType != XmlPullParser.END_DOCUMENT && !done) {
			String name = null;
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				// Create new List<RssMessage>
				break;
			case XmlPullParser.START_TAG:
				name = parser.getName();
				if (name.equalsIgnoreCase(ITEM)) {
					// create new RssMessage object
				} else {
					if (name.equalsIgnoreCase(LINK)) {
						Log.i("link", parser.nextText());
					} else if (name.equalsIgnoreCase(DESCRIPTION)) {
						Log.i("descr", parser.nextText());
					} else if (name.equalsIgnoreCase(PUB_DATE)) {
						Log.i("date", parser.nextText());
					} else if (name.equalsIgnoreCase(TITLE)) {
						Log.i("titel", parser.nextText());
					}
				}
				break;
			case XmlPullParser.END_TAG:
				name = parser.getName();
				if (name.equalsIgnoreCase(ITEM)) {
					// Add item
				} else if (name.equalsIgnoreCase(CHANNEL)) {
					done = true;
				}
				break;
			}
			eventType = parser.next();
		}
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 15
Source File: Dsmlv2ResponseParser.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Launches the parsing of the Batch Response only
 *
 * @throws XmlPullParserException if an error occurs in the parser
 */
public void parseBatchResponse() throws XmlPullParserException
{
    XmlPullParser xpp = container.getParser();

    int eventType = xpp.getEventType();
    
    do
    {
        switch ( eventType )
        {
            case XmlPullParser.START_DOCUMENT :
                container.setState( Dsmlv2StatesEnum.INIT_GRAMMAR_STATE );
                break;

            case XmlPullParser.END_DOCUMENT :
                container.setState( Dsmlv2StatesEnum.GRAMMAR_END );
                break;

            case XmlPullParser.START_TAG :
                processTag( container, Tag.START );
                break;

            case XmlPullParser.END_TAG :
                processTag( container, Tag.END );
                break;

            default:
                break;
        }
        
        try
        {
            eventType = xpp.next();
        }
        catch ( IOException ioe )
        {
            throw new XmlPullParserException( I18n.err( I18n.ERR_03019_IO_EXCEPTION_OCCURED, ioe.getLocalizedMessage() ), xpp, ioe );
        }
    }
    while ( container.getState() != Dsmlv2StatesEnum.BATCH_RESPONSE_LOOP );
}
 
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 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 17
Source File: DashManifestParser.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses an event object.
 *
 * @param xpp The current xml parser.
 * @param scratchOutputStream A {@link ByteArrayOutputStream} that's used when parsing the object.
 * @return The serialized byte array.
 * @throws XmlPullParserException If there is any error parsing this node.
 * @throws IOException If there is any error reading from the underlying input stream.
 */
protected byte[] parseEventObject(XmlPullParser xpp, ByteArrayOutputStream scratchOutputStream)
    throws XmlPullParserException, IOException {
  scratchOutputStream.reset();
  XmlSerializer xmlSerializer = Xml.newSerializer();
  xmlSerializer.setOutput(scratchOutputStream, C.UTF8_NAME);
  // Start reading everything between <Event> and </Event>, and serialize them into an Xml
  // byte array.
  xpp.nextToken();
  while (!XmlPullParserUtil.isEndTag(xpp, "Event")) {
    switch (xpp.getEventType()) {
      case (XmlPullParser.START_DOCUMENT):
        xmlSerializer.startDocument(null, false);
        break;
      case (XmlPullParser.END_DOCUMENT):
        xmlSerializer.endDocument();
        break;
      case (XmlPullParser.START_TAG):
        xmlSerializer.startTag(xpp.getNamespace(), xpp.getName());
        for (int i = 0; i < xpp.getAttributeCount(); i++) {
          xmlSerializer.attribute(xpp.getAttributeNamespace(i), xpp.getAttributeName(i),
              xpp.getAttributeValue(i));
        }
        break;
      case (XmlPullParser.END_TAG):
        xmlSerializer.endTag(xpp.getNamespace(), xpp.getName());
        break;
      case (XmlPullParser.TEXT):
        xmlSerializer.text(xpp.getText());
        break;
      case (XmlPullParser.CDSECT):
        xmlSerializer.cdsect(xpp.getText());
        break;
      case (XmlPullParser.ENTITY_REF):
        xmlSerializer.entityRef(xpp.getText());
        break;
      case (XmlPullParser.IGNORABLE_WHITESPACE):
        xmlSerializer.ignorableWhitespace(xpp.getText());
        break;
      case (XmlPullParser.PROCESSING_INSTRUCTION):
        xmlSerializer.processingInstruction(xpp.getText());
        break;
      case (XmlPullParser.COMMENT):
        xmlSerializer.comment(xpp.getText());
        break;
      case (XmlPullParser.DOCDECL):
        xmlSerializer.docdecl(xpp.getText());
        break;
      default: // fall out
    }
    xpp.nextToken();
  }
  xmlSerializer.flush();
  return scratchOutputStream.toByteArray();
}
 
Example 18
Source File: DashManifestParser.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses an event object.
 *
 * @param xpp The current xml parser.
 * @param scratchOutputStream A {@link ByteArrayOutputStream} that's used when parsing the object.
 * @return The serialized byte array.
 * @throws XmlPullParserException If there is any error parsing this node.
 * @throws IOException If there is any error reading from the underlying input stream.
 */
protected byte[] parseEventObject(XmlPullParser xpp, ByteArrayOutputStream scratchOutputStream)
    throws XmlPullParserException, IOException {
  scratchOutputStream.reset();
  XmlSerializer xmlSerializer = Xml.newSerializer();
  xmlSerializer.setOutput(scratchOutputStream, null);
  // Start reading everything between <Event> and </Event>, and serialize them into an Xml
  // byte array.
  xpp.nextToken();
  while (!XmlPullParserUtil.isEndTag(xpp, "Event")) {
    switch (xpp.getEventType()) {
      case (XmlPullParser.START_DOCUMENT):
        xmlSerializer.startDocument(null, false);
        break;
      case (XmlPullParser.END_DOCUMENT):
        xmlSerializer.endDocument();
        break;
      case (XmlPullParser.START_TAG):
        xmlSerializer.startTag(xpp.getNamespace(), xpp.getName());
        for (int i = 0; i < xpp.getAttributeCount(); i++) {
          xmlSerializer.attribute(xpp.getAttributeNamespace(i), xpp.getAttributeName(i),
              xpp.getAttributeValue(i));
        }
        break;
      case (XmlPullParser.END_TAG):
        xmlSerializer.endTag(xpp.getNamespace(), xpp.getName());
        break;
      case (XmlPullParser.TEXT):
        xmlSerializer.text(xpp.getText());
        break;
      case (XmlPullParser.CDSECT):
        xmlSerializer.cdsect(xpp.getText());
        break;
      case (XmlPullParser.ENTITY_REF):
        xmlSerializer.entityRef(xpp.getText());
        break;
      case (XmlPullParser.IGNORABLE_WHITESPACE):
        xmlSerializer.ignorableWhitespace(xpp.getText());
        break;
      case (XmlPullParser.PROCESSING_INSTRUCTION):
        xmlSerializer.processingInstruction(xpp.getText());
        break;
      case (XmlPullParser.COMMENT):
        xmlSerializer.comment(xpp.getText());
        break;
      case (XmlPullParser.DOCDECL):
        xmlSerializer.docdecl(xpp.getText());
        break;
      default: // fall out
    }
    xpp.nextToken();
  }
  xmlSerializer.flush();
  return scratchOutputStream.toByteArray();
}
 
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 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 20
Source File: ManifestReader.java    From PluginLoader with Apache License 2.0 4 votes vote down vote up
public static String getManifestXMLFromAPK(ZipFile file, ZipEntry entry) {
    StringBuilder xmlSb = new StringBuilder(100);
    XmlResourceParser parser = null;
    try {
        parser = new XmlResourceParser();
        parser.open(file.getInputStream(entry));

        StringBuilder sb = new StringBuilder(10);
        final String indentStep = " ";

        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            switch (type) {
                case XmlPullParser.START_DOCUMENT: {
                    log(xmlSb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    break;
                }
                case XmlPullParser.START_TAG: {
                    log(false, xmlSb, "%s<%s%s", sb, getNamespacePrefix(parser.getPrefix()), parser.getName());
                    sb.append(indentStep);

                    int namespaceCountBefore = parser.getNamespaceCount(parser.getDepth() - 1);
                    int namespaceCount = parser.getNamespaceCount(parser.getDepth());

                    for (int i = namespaceCountBefore; i != namespaceCount; ++i) {
                        log(xmlSb, "%sxmlns:%s=\"%s\"", i == namespaceCountBefore ? "  " : sb,
                            parser.getNamespacePrefix(i), parser.getNamespaceUri(i));
                    }

                    for (int i = 0, size = parser.getAttributeCount(); i != size; ++i) {
                        log(false, xmlSb, "%s%s%s=\"%s\"", " ", getNamespacePrefix(parser.getAttributePrefix(i)),
                            parser.getAttributeName(i), getAttributeValue(parser, i));
                    }
                    // log("%s>",sb);
                    log(xmlSb, ">");
                    break;
                }
                case XmlPullParser.END_TAG: {
                    sb.setLength(sb.length() - indentStep.length());
                    log(xmlSb, "%s</%s%s>", sb, getNamespacePrefix(parser.getPrefix()), parser.getName());
                    break;
                }
                case XmlPullParser.TEXT: {
                    log(xmlSb, "%s%s", sb, parser.getText());
                    break;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        parser.close();
    }
    return xmlSb.toString();
}