1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package cgeo.geocaching.utils;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
public final class XmlUtils {
private XmlUtils() {
// Do not instantiate
}
/**
* Insert an attribute-less tag with enclosed text in a XML serializer output.
*
* @param serializer an XML serializer
* @param prefix an XML prefix, see {@link XmlSerializer#startTag(String, String)}
* @param tag an XML tag
* @param text some text to insert, or <tt>null</tt> to omit completely this tag
* @throws IOException
*/
public static void simpleText(final XmlSerializer serializer, final String prefix, final String tag, final String text) throws IOException {
if (text != null) {
serializer.startTag(prefix, tag);
serializer.text(text);
serializer.endTag(prefix, tag);
}
}
/**
* Insert pairs of attribute-less tags and enclosed texts in a XML serializer output
*
* @param serializer an XML serializer
* @param prefix an XML prefix, see {@link XmlSerializer#startTag(String, String)} shared by all tags
* @param tagAndText an XML tag, the corresponding text, another XML tag, the corresponding text. <tt>null</tt> texts
* will be omitted along with their respective tag.
* @throws IOException
*/
public static void multipleTexts(final XmlSerializer serializer, final String prefix, final String... tagAndText) throws IOException {
for (int i = 0; i < tagAndText.length; i += 2) {
simpleText(serializer, prefix, tagAndText[i], tagAndText[i+1]);
}
}
}
|