Android XML Serializer Write writeMapXml(Map val, String name, XmlSerializer out)

Here you can find the source of writeMapXml(Map val, String name, XmlSerializer out)

Description

Flatten a Map into an XmlSerializer.

License

Apache License

Parameter

Parameter Description
val The map to be flattened.
name Name attribute to include with this list's tag, or null for none.
out XmlSerializer to write the map into.

Declaration

public static final void writeMapXml(Map val, String name,
        XmlSerializer out) throws XmlPullParserException,
        java.io.IOException 

Method Source Code

//package com.java2s;
/*// ww  w.  j  a  v  a2s. c o  m
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;

import java.io.OutputStream;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    /**
     * Flatten a Map into an XmlSerializer. The map can later be read back with
     * readThisMapXml().
     * 
     * @param val The map to be flattened.
     * @param name Name attribute to include with this list's tag, or null for
     *            none.
     * @param out XmlSerializer to write the map into.
     * @see #writeMapXml(Map, OutputStream)
     * @see #writeListXml
     * @see #writeValueXml
     * @see #readMapXml
     */
    public static final void writeMapXml(Map val, String name,
            XmlSerializer out) throws XmlPullParserException,
            java.io.IOException {
        if (val == null) {
            out.startTag(null, "null");
            out.endTag(null, "null");
            return;
        }

        Set s = val.entrySet();
        Iterator i = s.iterator();

        out.startTag(null, "map");
        if (name != null) {
            out.attribute(null, "name", name);
        }

        while (i.hasNext()) {
            Map.Entry e = (Map.Entry) i.next();
            writeValueXml(e.getValue(), (String) e.getKey(), out);
        }

        out.endTag(null, "map");
    }

    /**
     * Flatten an object's value into an XmlSerializer. The value can later be
     * read back with readThisValueXml(). Currently supported value types are:
     * null, String, Integer, Long, Float, Double Boolean, Map, List.
     * 
     * @param v The object to be flattened.
     * @param name Name attribute to include with this value's tag, or null for
     *            none.
     * @param out XmlSerializer to write the object into.
     * @see #writeMapXml
     * @see #writeListXml
     * @see #readValueXml
     */
    public static final void writeValueXml(Object v, String name,
            XmlSerializer out) throws XmlPullParserException,
            java.io.IOException {
        String typeStr;
        if (v == null) {
            out.startTag(null, "null");
            if (name != null) {
                out.attribute(null, "name", name);
            }
            out.endTag(null, "null");
            return;
        } else if (v instanceof String) {
            out.startTag(null, "string");
            if (name != null) {
                out.attribute(null, "name", name);
            }
            out.text(v.toString());
            out.endTag(null, "string");
            return;
        } else if (v instanceof Integer) {
            typeStr = "int";
        } else if (v instanceof Long) {
            typeStr = "long";
        } else if (v instanceof Float) {
            typeStr = "float";
        } else if (v instanceof Double) {
            typeStr = "double";
        } else if (v instanceof Boolean) {
            typeStr = "boolean";
        } else if (v instanceof byte[]) {
            writeByteArrayXml((byte[]) v, name, out);
            return;
        } else if (v instanceof int[]) {
            writeIntArrayXml((int[]) v, name, out);
            return;
        } else if (v instanceof Map) {
            writeMapXml((Map) v, name, out);
            return;
        } else if (v instanceof List) {
            writeListXml((List) v, name, out);
            return;
        } else if (v instanceof CharSequence) {
            // XXX This is to allow us to at least write something if
            // we encounter styled text... but it means we will drop all
            // of the styling information. :(
            out.startTag(null, "string");
            if (name != null) {
                out.attribute(null, "name", name);
            }
            out.text(v.toString());
            out.endTag(null, "string");
            return;
        } else {
            throw new RuntimeException(
                    "writeValueXml: unable to write value " + v);
        }

        out.startTag(null, typeStr);
        if (name != null) {
            out.attribute(null, "name", name);
        }
        out.attribute(null, "value", v.toString());
        out.endTag(null, typeStr);
    }

    /**
     * Flatten a byte[] into an XmlSerializer. The list can later be read back
     * with readThisByteArrayXml().
     * 
     * @param val The byte array to be flattened.
     * @param name Name attribute to include with this array's tag, or null for
     *            none.
     * @param out XmlSerializer to write the array into.
     * @see #writeMapXml
     * @see #writeValueXml
     */
    public static final void writeByteArrayXml(byte[] val, String name,
            XmlSerializer out) throws XmlPullParserException,
            java.io.IOException {

        if (val == null) {
            out.startTag(null, "null");
            out.endTag(null, "null");
            return;
        }

        out.startTag(null, "byte-array");
        if (name != null) {
            out.attribute(null, "name", name);
        }

        final int N = val.length;
        out.attribute(null, "num", Integer.toString(N));

        StringBuilder sb = new StringBuilder(val.length * 2);
        for (int i = 0; i < N; i++) {
            int b = val[i];
            int h = b >> 4;
            sb.append(h >= 10 ? ('a' + h - 10) : ('0' + h));
            h = b & 0xff;
            sb.append(h >= 10 ? ('a' + h - 10) : ('0' + h));
        }

        out.text(sb.toString());

        out.endTag(null, "byte-array");
    }

    /**
     * Flatten an int[] into an XmlSerializer. The list can later be read back
     * with readThisIntArrayXml().
     * 
     * @param val The int array to be flattened.
     * @param name Name attribute to include with this array's tag, or null for
     *            none.
     * @param out XmlSerializer to write the array into.
     * @see #writeMapXml
     * @see #writeValueXml
     * @see #readThisIntArrayXml
     */
    public static final void writeIntArrayXml(int[] val, String name,
            XmlSerializer out) throws XmlPullParserException,
            java.io.IOException {

        if (val == null) {
            out.startTag(null, "null");
            out.endTag(null, "null");
            return;
        }

        out.startTag(null, "int-array");
        if (name != null) {
            out.attribute(null, "name", name);
        }

        final int N = val.length;
        out.attribute(null, "num", Integer.toString(N));

        for (int i = 0; i < N; i++) {
            out.startTag(null, "item");
            out.attribute(null, "value", Integer.toString(val[i]));
            out.endTag(null, "item");
        }

        out.endTag(null, "int-array");
    }

    /**
     * Flatten a List into an output stream as XML. The list can later be read
     * back with readListXml().
     * 
     * @param val The list to be flattened.
     * @param out Where to write the XML data.
     * @see #writeListXml(List, String, XmlSerializer)
     * @see #writeMapXml
     * @see #writeValueXml
     * @see #readListXml
     */
    public static final void writeListXml(List val, OutputStream out)
            throws XmlPullParserException, java.io.IOException {
        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(out, "utf-8");
        serializer.startDocument(null, true);
        serializer.setFeature(
                "http://xmlpull.org/v1/doc/features.html#indent-output",
                true);
        writeListXml(val, null, serializer);
        serializer.endDocument();
    }

    /**
     * Flatten a List into an XmlSerializer. The list can later be read back
     * with readThisListXml().
     * 
     * @param val The list to be flattened.
     * @param name Name attribute to include with this list's tag, or null for
     *            none.
     * @param out XmlSerializer to write the list into.
     * @see #writeListXml(List, OutputStream)
     * @see #writeMapXml
     * @see #writeValueXml
     * @see #readListXml
     */
    public static final void writeListXml(List val, String name,
            XmlSerializer out) throws XmlPullParserException,
            java.io.IOException {
        if (val == null) {
            out.startTag(null, "null");
            out.endTag(null, "null");
            return;
        }

        out.startTag(null, "list");
        if (name != null) {
            out.attribute(null, "name", name);
        }

        int N = val.size();
        int i = 0;
        while (i < N) {
            writeValueXml(val.get(i), null, out);
            i++;
        }

        out.endTag(null, "list");
    }
}

Related

  1. writeLongAttribute(XmlSerializer out, String name, long value)
  2. writeMapXml(Map val, String name, XmlSerializer out)
  3. writeMapXml(Map val, String name, XmlSerializer out)
  4. writeMapXml(Map val, String name, XmlSerializer out)
  5. writeMapXml(Map val, String name, XmlSerializer out)
  6. writeSetXml(Set val, String name, XmlSerializer out)
  7. writeSetXml(Set val, String name, XmlSerializer out)
  8. writeSetXml(Set val, String name, XmlSerializer out)
  9. writeSimpleElement(XmlSerializer serializer, String namespace, String elementName, String elementText)