Android Open Source - LCFR-Mobile-Android Protocol Xml Parser






From Project

Back to project page LCFR-Mobile-Android.

License

The source code is released under:

Apache License

If you think the Android project LCFR-Mobile-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.dizon.lcfr;
//from   www . j a  va2  s  .  c o m
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

public class ProtocolXmlParser {
  // We don't use namespaces
    private static final String ns = null;
   
    public List<Protocol> parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readProtocolXML(parser);
        } finally {
            in.close();
        }
    }
    
    private List<Protocol> readProtocolXML(XmlPullParser parser) throws XmlPullParserException, IOException {
        List<Protocol> entries = new ArrayList<Protocol>();

        parser.require(XmlPullParser.START_TAG, ns, "ArrayOfProtocol");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Starts by looking for the entry tag
            if (name.equals("Protocol")) {
                entries.add(readProtocol(parser));
            } else {
                skip(parser);
            }
        }  
        return entries;
    }
    
 // Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them off
 // to their respective "read" methods for processing. Otherwise, skips the tag.
 private Protocol readProtocol(XmlPullParser parser) throws XmlPullParserException, IOException {
     parser.require(XmlPullParser.START_TAG, ns, "Protocol");
     Protocol proto = new Protocol();
     while (parser.next() != XmlPullParser.END_TAG) {
         if (parser.getEventType() != XmlPullParser.START_TAG) {
             continue;
         }
         String name = parser.getName();
         if (name.equals("Name")) {
             proto.setName(readName(parser));
         } else if (name.equals("TocIndex")) {
             proto.setTocIndex(readTocIndex(parser));
         } else if (name.equals("Files")) {
           parser.require(XmlPullParser.START_TAG, ns, "Files");
             while (parser.next() != XmlPullParser.END_TAG) {
                 if (parser.getEventType() != XmlPullParser.START_TAG) {
                     continue;
                 }
                 String innername = parser.getName();
                 // Starts by looking for the entry tag
                 if (innername.equals("ProtocolFile")) {
                   proto.getFiles().add(readFile(parser));;
                 } else {
                     skip(parser);
                 }
             }  
         } else {
             skip(parser);
         }
     }
     return proto;
 }

 // Processes title tags in the feed.
 private String readName(XmlPullParser parser) throws IOException, XmlPullParserException {
     parser.require(XmlPullParser.START_TAG, ns, "Name");
     String tag = readText(parser);
     parser.require(XmlPullParser.END_TAG, ns, "Name");
     return tag;
 }
   
 // Processes link tags in the feed.
 private int readTocIndex(XmlPullParser parser) throws IOException, XmlPullParserException {
     parser.require(XmlPullParser.START_TAG, ns, "TocIndex");
     String tag = readText(parser);
     parser.require(XmlPullParser.END_TAG, ns, "TocIndex");
     return Integer.parseInt(tag);
 }

 // Processes summary tags in the feed.
 private ProtocolFile readFile(XmlPullParser parser) throws IOException, XmlPullParserException {
     ProtocolFile file = new ProtocolFile();
   parser.require(XmlPullParser.START_TAG, ns, "ProtocolFile");
     while (parser.next() != XmlPullParser.END_TAG) {
         if (parser.getEventType() != XmlPullParser.START_TAG) {
             continue;
         }
         
         String name = parser.getName();
         if (name.equals("FileHash")) {
             file.setFileHash(readHash(parser));
         } else if (name.equals("FileName")) {
             file.setFileName(readFileName(parser));
         } else {
             skip(parser);
         }
     }
     parser.require(XmlPullParser.END_TAG, ns, "ProtocolFile");
     return file;
 }
 
 // Processes title tags in the feed.
 private String readHash(XmlPullParser parser) throws IOException, XmlPullParserException {
     parser.require(XmlPullParser.START_TAG, ns, "FileHash");
     String tag = readText(parser);
     parser.require(XmlPullParser.END_TAG, ns, "FileHash");
     return tag;
 }
 
 // Processes title tags in the feed.
 private String readFileName(XmlPullParser parser) throws IOException, XmlPullParserException {
     parser.require(XmlPullParser.START_TAG, ns, "FileName");
     String tag = readText(parser);
     parser.require(XmlPullParser.END_TAG, ns, "FileName");
     return tag;
 }

 // For the tags title and summary, extracts their text values.
 private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
     String result = "";
     if (parser.next() == XmlPullParser.TEXT) {
         result = parser.getText();
         parser.nextTag();
     }
     return result;
 }
 
 private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
      if (parser.getEventType() != XmlPullParser.START_TAG) {
          throw new IllegalStateException();
      }
      int depth = 1;
      while (depth != 0) {
          switch (parser.next()) {
          case XmlPullParser.END_TAG:
              depth--;
              break;
          case XmlPullParser.START_TAG:
              depth++;
              break;
          }
      }
   }

}




Java Source Code List

org.dizon.LCFRCAD.CADData.java
org.dizon.LCFRCAD.Calls.java
org.dizon.LCFRCAD.Pcalls.java
org.dizon.lcfr.ProtocolContent.java
org.dizon.lcfr.ProtocolFile.java
org.dizon.lcfr.ProtocolXmlParser.java
org.dizon.lcfr.Protocol.java
org.dizon.lcfrmobile.GetS3File.java
org.dizon.lcfrmobile.HttpTeste.java
org.dizon.lcfrmobile.IncidentDetailActivity.java
org.dizon.lcfrmobile.IncidentDetailFragment.java
org.dizon.lcfrmobile.IncidentListActivity.java
org.dizon.lcfrmobile.IncidentListFragment.java
org.dizon.lcfrmobile.MainActivity.java
org.dizon.lcfrmobile.MainDisplayFragment.java
org.dizon.lcfrmobile.OnDownloadNotify.java
org.dizon.lcfrmobile.ProtocolDetailActivity.java
org.dizon.lcfrmobile.ProtocolDetailFragment.java
org.dizon.lcfrmobile.ProtocolListActivity.java
org.dizon.lcfrmobile.ProtocolListFragment.java
org.dizon.lcfrmobile.S3IndexChecker.java
org.dizon.lcfrmobile.SettingsActivity.java
org.dizon.lcfrmobile.dummy.DummyContent.java