Java Code Examples for org.xmlpull.v1.XmlPullParser#nextToken()

The following examples show how to use org.xmlpull.v1.XmlPullParser#nextToken() . 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: WeChatHelper.java    From WechatHook-Dusan with Apache License 2.0 6 votes vote down vote up
public static String getFromXml(String xmlmsg, String node) throws XmlPullParserException, IOException {
    String xl = xmlmsg.substring(xmlmsg.indexOf("<msg>"));
    //nativeurl
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser pz = factory.newPullParser();
    pz.setInput(new StringReader(xl));
    int eventType = pz.getEventType();
    String result = "";
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            if (pz.getName().equals(node)) {
                pz.nextToken();
                result = pz.getText();
                break;
            }
        }
        eventType = pz.next();
    }
    return result;
}
 
Example 2
Source File: Main.java    From WeChatLuckyMoney with Apache License 2.0 6 votes vote down vote up
private String getFromXml(String xmlmsg, String node) throws XmlPullParserException, IOException {
    String xl = xmlmsg.substring(xmlmsg.indexOf("<msg>"));
    //nativeurl
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser pz = factory.newPullParser();
    pz.setInput(new StringReader(xl));
    int v = pz.getEventType();
    String result = "";
    while (v != XmlPullParser.END_DOCUMENT) {
        if (v == XmlPullParser.START_TAG) {
            if (pz.getName().equals(node)) {
                pz.nextToken();
                result = pz.getText();
                break;
            }
        }
        v = pz.next();
    }
    return result;
}
 
Example 3
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 4
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 5
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
protected int next(XmlPullParser xpp) throws XmlPullParserException, IOException {
	if (handleComments)
		return xpp.nextToken();
	else
		return xpp.next();    
}
 
Example 6
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
protected int next(XmlPullParser xpp) throws XmlPullParserException, IOException {
	if (handleComments)
		return xpp.nextToken();
	else
		return xpp.next();    
}
 
Example 7
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
protected int next(XmlPullParser xpp) throws XmlPullParserException, IOException {
	if (handleComments)
		return xpp.nextToken();
	else
		return xpp.next();    
}
 
Example 8
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
protected int next(XmlPullParser xpp) throws XmlPullParserException, IOException {
	if (handleComments)
		return xpp.nextToken();
	else
		return xpp.next();    
}
 
Example 9
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
protected int next(XmlPullParser xpp) throws XmlPullParserException, IOException {
	if (handleComments)
		return xpp.nextToken();
	else
		return xpp.next();    
}
 
Example 10
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 11
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 12
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 13
Source File: CustomImageView.java    From microMathematics with GNU General Public License v3.0 4 votes vote down vote up
public void readFromXml(XmlPullParser parser)
{
    clear();
    try
    {
        // On android version < 11, the standard XmlPullParser implementation has a bug:
        // it throws a UnsupportedOperationException at getting CDSECT using nextToken().
        // Therefore, XmlPullParserFactory.newInstance().newPullParser() shall be used
        // to create parser instance in order to get it worked on Android versions 8-10.
        final String type = parser.getAttributeValue(null, XML_PROP_IMAGE_TYPE);
        if (type == null)
        {
            throw new Exception("image type is unknown");
        }
        final int event = parser.nextToken();
        if (event == XmlPullParser.CDSECT)
        {
            final String imageText = parser.getText();
            if (imageText == null || imageText.length() == 0)
            {
                throw new Exception("empty CDSECT");
            }
            byte[] imageDecoded = Base64.decode(imageText, BASE64_OPTIONS);
            if (imageDecoded == null)
            {
                throw new Exception("cannot decode image, string lenght = " + imageText.length());
            }
            if (XML_PROP_IMAGE_PNG.equalsIgnoreCase(type))
            {
                ByteArrayInputStream imageStream = new ByteArrayInputStream(imageDecoded);
                setBitmap(BitmapFactory.decodeStream(imageStream));
                imageStream.close();
            }
            else if (XML_PROP_IMAGE_SVG.equalsIgnoreCase(type))
            {
                setSvg(new String(imageDecoded));
            }
        }
        else
        {
            throw new Exception("CDSECT is not found");
        }
    }
    catch (Exception e)
    {
        ViewUtils.Debug(this, e.getLocalizedMessage());
    }
}
 
Example 14
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 15
Source File: DashManifestParser.java    From Telegram 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();
}