Android Open Source - android-tools X M L Parser






From Project

Back to project page android-tools.

License

The source code is released under:

MIT License

If you think the Android project android-tools 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 net.comfreeze.lib.xml;
/*from   www  .  ja v  a2s .c  o  m*/
import android.util.Log;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

abstract public class XMLParser extends DefaultHandler {
    private static final String TAG = XMLParser.class.getSimpleName();

    protected InputStream source = null;

    protected StringBuffer buff = null;

    protected boolean buffering = false;

    protected int currentIndex;

    public static boolean silent = true;//CFZApplication.silent;

    public XMLParser() {
        if (!silent)
            Log.d(TAG, "Initalizing");
        currentIndex = 0;
    }

    public XMLParser(InputStream in) {
        if (!silent)
            Log.d(TAG, "Initalizing");
        currentIndex = 0;
        source = in;
    }

    public XMLParser setSource(InputStream source) {
        this.source = source;
        return this;
    }

    public void process() {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = null;
        try {
            sp = spf.newSAXParser();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        XMLReader xr = null;
        try {
            xr = sp.getXMLReader();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        xr.setContentHandler(this);
        try {
            xr.parse(new InputSource(source));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    /**
     * Processes each element encountered, in order discovered in the source
     * document.
     *
     * @param namespace
     * @param element
     * @param qualifiedName
     * @param attributes
     */
    protected abstract void processElement(String namespace, String element, String qualifiedName, Attributes attributes);

    /**
     * Finalizes each element encountered, in order discovered in the source
     * document.
     *
     * @param namespace
     * @param element
     * @param qualifiedName
     */
    protected abstract void finishElement(String namespace, String element, String qualifiedName);

    @Override
    public void startDocument() throws SAXException {
        if (!silent)
            Log.d(TAG, "Parsing document");
        // Initialization of parsing for document
        currentIndex = 0;
    }

    @Override
    public void endDocument() throws SAXException {
        if (!silent)
            Log.d(TAG, "Closing document");
        // Finalization logic upon completion of parsing
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException {
        if (!silent)
            Log.d(TAG, "Starting element: " + namespaceURI + ":" + localName);
        this.processElement(namespaceURI, localName, qName, attrs);
    }

    @Override
    public void characters(char ch[], int start, int length) {
        if (buffering) {
            if (!silent)
                Log.d(TAG, "Buffering characters");
            if (buff == null) {
                buff = new StringBuffer();
            }
            buff.append(ch, start, length);
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        if (!silent)
            Log.d(TAG, "Finished element: " + namespaceURI + ":" + localName);
        finishElement(namespaceURI, localName, qName);
    }

    protected void log(String message) {
        if (!silent)
            Log.d(TAG, message);
    }
}




Java Source Code List

net.comfreeze.lib.BundleBuilder.java
net.comfreeze.lib.CFZApplication.java
net.comfreeze.lib.ContentValueBuilder.java
net.comfreeze.lib.FragmentMap.java
net.comfreeze.lib.adapter.HeaderListAdapter.java
net.comfreeze.lib.adapter.IHeaderListAdapter.java
net.comfreeze.lib.adapter.SeparatedListAdapter.java
net.comfreeze.lib.api.BaseAPI.java
net.comfreeze.lib.api.RestAPI.java
net.comfreeze.lib.api.XMLAPI.java
net.comfreeze.lib.api.helper.CursorHelper.java
net.comfreeze.lib.api.helper.JSONHelper.java
net.comfreeze.lib.api.helper.ModelHelper.java
net.comfreeze.lib.api.xml.WordpressAPI.java
net.comfreeze.lib.audio.SoundManager.java
net.comfreeze.lib.db.DatabaseHelper.java
net.comfreeze.lib.db.DatabaseTable.java
net.comfreeze.lib.db.helper.HelperCursor.java
net.comfreeze.lib.db.model.CFZModel.java
net.comfreeze.lib.db.model.FieldColumnMap.java
net.comfreeze.lib.fragments.CFZListFragment.java
net.comfreeze.lib.provider.CFZSimpleProvider.java
net.comfreeze.lib.service.CFZService.java
net.comfreeze.lib.ui.SupportFragmentActivity.java
net.comfreeze.lib.ui.dialog.CFZDialogProgress.java
net.comfreeze.lib.ui.fragment.CFZFragmentBase.java
net.comfreeze.lib.views.BiScrollView.java
net.comfreeze.lib.views.CFZViewHelper.java
net.comfreeze.lib.views.FlowLayout.java
net.comfreeze.lib.views.GestureHelper.java
net.comfreeze.lib.views.HeaderListView.java
net.comfreeze.lib.views.ResizingView.java
net.comfreeze.lib.views.ViewCollection.java
net.comfreeze.lib.views.ViewUtils.java
net.comfreeze.lib.xml.XMLParser.java
net.comfreeze.lib.xml.wordpress.FeedXmlParser.java