Search in sources :

Example 1 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project cw-advandroid by commonsguy.

the class LoremBase method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection = (TextView) findViewById(R.id.selection);
    try {
        XmlPullParser xpp = getResources().getXml(R.xml.words);
        while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
            if (xpp.getEventType() == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("word")) {
                    items.add(xpp.getAttributeValue(0));
                }
            }
            xpp.next();
        }
    } catch (Throwable t) {
        Toast.makeText(this, "Request failed: " + t.toString(), 4000).show();
    }
    setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
    onNewIntent(getIntent());
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser)

Example 2 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project cw-android by commonsguy.

the class XMLResourceDemo method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection = (TextView) findViewById(R.id.selection);
    try {
        XmlPullParser xpp = getResources().getXml(R.xml.words);
        while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
            if (xpp.getEventType() == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("word")) {
                    items.add(xpp.getAttributeValue(0));
                }
            }
            xpp.next();
        }
    } catch (Throwable t) {
        Toast.makeText(this, "Request failed: " + t.toString(), Toast.LENGTH_LONG).show();
    }
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser)

Example 3 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project GreenDroid by cyrilmottier.

the class ItemAdapter method createFromXml.

/**
     * Creates an ItemAdapter from a given XML document. Called on a parser
     * positioned at a tag in an XML document, tries to create an ItemAdapter
     * from that tag.
     * 
     * @param context The Context in which the ItemAdapter will be used in
     * @param parser The XmlPullParser
     * @return a new ItemAdapter constructed with the content of the file
     *         pointed by <em>xmlId</em>
     * @throws XmlPullParserException
     * @throws IOException
     */
public static ItemAdapter createFromXml(Context context, XmlPullParser parser) throws XmlPullParserException, IOException {
    AttributeSet attrs = Xml.asAttributeSet(parser);
    int type;
    while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
    // Empty loop
    }
    if (type != XmlPullParser.START_TAG) {
        throw new XmlPullParserException("No start tag found");
    }
    if (!parser.getName().equals("item-array")) {
        throw new XmlPullParserException("Unknown start tag. Should be 'item-array'");
    }
    final List<Item> items = new ArrayList<Item>();
    final int innerDepth = parser.getDepth() + 1;
    final Resources r = context.getResources();
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        if (depth > innerDepth) {
            continue;
        }
        String name = parser.getName();
        Item item;
        if (name.equals("text-item")) {
            item = new TextItem();
        } else if (name.equals("longtext-item")) {
            item = new LongTextItem();
        } else if (name.equals("description-item")) {
            item = new DescriptionItem();
        } else if (name.equals("separator-item")) {
            item = new SeparatorItem();
        } else if (name.equals("progress-item")) {
            item = new ProgressItem();
        } else if (name.equals("drawable-item")) {
            item = new DrawableItem();
        } else if (name.equals("subtitle-item")) {
            item = new SubtitleItem();
        } else if (name.equals("subtext-item")) {
            item = new SubtextItem();
        } else if (name.equals("thumbnail-item")) {
            item = new ThumbnailItem();
        } else {
            // ItemAdapter and creates our own items via XML?
            throw new XmlPullParserException(parser.getPositionDescription() + ": invalid item tag " + name);
        }
        if (item != null) {
            item.inflate(r, parser, attrs);
            items.add(item);
        }
    }
    return new ItemAdapter(context, items);
}
Also used : SubtitleItem(greendroid.widget.item.SubtitleItem) LongTextItem(greendroid.widget.item.LongTextItem) TextItem(greendroid.widget.item.TextItem) ArrayList(java.util.ArrayList) ProgressItem(greendroid.widget.item.ProgressItem) DrawableItem(greendroid.widget.item.DrawableItem) Item(greendroid.widget.item.Item) LongTextItem(greendroid.widget.item.LongTextItem) TextItem(greendroid.widget.item.TextItem) DescriptionItem(greendroid.widget.item.DescriptionItem) ThumbnailItem(greendroid.widget.item.ThumbnailItem) SeparatorItem(greendroid.widget.item.SeparatorItem) SubtitleItem(greendroid.widget.item.SubtitleItem) SubtextItem(greendroid.widget.item.SubtextItem) DrawableItem(greendroid.widget.item.DrawableItem) ProgressItem(greendroid.widget.item.ProgressItem) SubtextItem(greendroid.widget.item.SubtextItem) AttributeSet(android.util.AttributeSet) ThumbnailItem(greendroid.widget.item.ThumbnailItem) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) LongTextItem(greendroid.widget.item.LongTextItem) SeparatorItem(greendroid.widget.item.SeparatorItem) DescriptionItem(greendroid.widget.item.DescriptionItem)

Example 4 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project android-job by evernote.

the class XmlUtils method readThisLongArrayXml.

/**
     * Read a long[] object from an XmlPullParser.  The XML data could
     * previously have been generated by writeLongArrayXml().  The XmlPullParser
     * must be positioned <em>after</em> the tag that begins the list.
     *
     * @param parser The XmlPullParser from which to read the list data.
     * @param endTag Name of the tag that will end the list, usually "list".
     * @param name An array of one string, used to return the name attribute
     *             of the list's tag.
     *
     * @return Returns a newly generated long[].
     *
     * @see #readListXml
     */
public static final long[] readThisLongArrayXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, java.io.IOException {
    int num;
    try {
        num = Integer.parseInt(parser.getAttributeValue(null, "num"));
    } catch (NullPointerException e) {
        throw new XmlPullParserException("Need num attribute in long-array");
    } catch (NumberFormatException e) {
        throw new XmlPullParserException("Not a number in num attribute in long-array");
    }
    parser.next();
    long[] array = new long[num];
    int i = 0;
    int eventType = parser.getEventType();
    do {
        if (eventType == parser.START_TAG) {
            if (parser.getName().equals("item")) {
                try {
                    array[i] = Long.parseLong(parser.getAttributeValue(null, "value"));
                } catch (NullPointerException e) {
                    throw new XmlPullParserException("Need value attribute in item");
                } catch (NumberFormatException e) {
                    throw new XmlPullParserException("Not a number in value attribute in item");
                }
            } else {
                throw new XmlPullParserException("Expected item tag at: " + parser.getName());
            }
        } else if (eventType == parser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return array;
            } else if (parser.getName().equals("item")) {
                i++;
            } else {
                throw new XmlPullParserException("Expected " + endTag + " end tag at: " + parser.getName());
            }
        }
        eventType = parser.next();
    } while (eventType != parser.END_DOCUMENT);
    throw new XmlPullParserException("Document ended before " + endTag + " end tag");
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 5 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project android-job by evernote.

the class XmlUtils method readThisValueXml.

private static final Object readThisValueXml(XmlPullParser parser, String[] name, ReadMapCallback callback) throws XmlPullParserException, java.io.IOException {
    final String valueName = parser.getAttributeValue(null, "name");
    final String tagName = parser.getName();
    //System.out.println("Reading this value tag: " + tagName + ", name=" + valueName);
    Object res;
    if (tagName.equals("null")) {
        res = null;
    } else if (tagName.equals("string")) {
        String value = "";
        int eventType;
        while ((eventType = parser.next()) != parser.END_DOCUMENT) {
            if (eventType == parser.END_TAG) {
                if (parser.getName().equals("string")) {
                    name[0] = valueName;
                    //System.out.println("Returning value for " + valueName + ": " + value);
                    return value;
                }
                throw new XmlPullParserException("Unexpected end tag in <string>: " + parser.getName());
            } else if (eventType == parser.TEXT) {
                value += parser.getText();
            } else if (eventType == parser.START_TAG) {
                throw new XmlPullParserException("Unexpected start tag in <string>: " + parser.getName());
            }
        }
        throw new XmlPullParserException("Unexpected end of document in <string>");
    } else if ((res = readThisPrimitiveValueXml(parser, tagName)) != null) {
    // all work already done by readThisPrimitiveValueXml
    } else if (tagName.equals("int-array")) {
        res = readThisIntArrayXml(parser, "int-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("long-array")) {
        res = readThisLongArrayXml(parser, "long-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("double-array")) {
        res = readThisDoubleArrayXml(parser, "double-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("string-array")) {
        res = readThisStringArrayXml(parser, "string-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("map")) {
        parser.next();
        res = readThisMapXml(parser, "map", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("list")) {
        parser.next();
        res = readThisListXml(parser, "list", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("set")) {
        parser.next();
        res = readThisSetXml(parser, "set", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (callback != null) {
        res = callback.readThisUnknownObjectXml(parser, tagName);
        name[0] = valueName;
        return res;
    } else {
        throw new XmlPullParserException("Unknown tag: " + tagName);
    }
    // Skip through to end tag.
    int eventType;
    while ((eventType = parser.next()) != parser.END_DOCUMENT) {
        if (eventType == parser.END_TAG) {
            if (parser.getName().equals(tagName)) {
                name[0] = valueName;
                //System.out.println("Returning value for " + valueName + ": " + res);
                return res;
            }
            throw new XmlPullParserException("Unexpected end tag in <" + tagName + ">: " + parser.getName());
        } else if (eventType == parser.TEXT) {
            throw new XmlPullParserException("Unexpected text in <" + tagName + ">: " + parser.getName());
        } else if (eventType == parser.START_TAG) {
            throw new XmlPullParserException("Unexpected start tag in <" + tagName + ">: " + parser.getName());
        }
    }
    throw new XmlPullParserException("Unexpected end of document in <" + tagName + ">");
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

XmlPullParser (org.xmlpull.v1.XmlPullParser)665 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)614 IOException (java.io.IOException)376 FileNotFoundException (java.io.FileNotFoundException)185 FileInputStream (java.io.FileInputStream)184 File (java.io.File)107 ArrayList (java.util.ArrayList)75 StringReader (java.io.StringReader)65 AttributeSet (android.util.AttributeSet)61 Test (org.junit.Test)57 TypedArray (android.content.res.TypedArray)56 InputStream (java.io.InputStream)48 AtomicFile (android.util.AtomicFile)47 HashMap (java.util.HashMap)42 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)39 FileReader (java.io.FileReader)36 BufferedInputStream (java.io.BufferedInputStream)30 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)29 RemoteException (android.os.RemoteException)28 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)28