1 // Copyright 2007 The Android Open Source Project 2 3 package com.google.wireless.gdata2.data; 4 5 import org.xmlpull.v1.XmlPullParser; 6 import org.xmlpull.v1.XmlPullParserException; 7 import org.xmlpull.v1.XmlSerializer; 8 9 import java.io.IOException; 10 11 /** 12 * Utility class for working with an XmlPullParser. 13 */ 14 public final class XmlUtils { 15 // utility class XmlUtils()16 private XmlUtils() { 17 } 18 19 /** 20 * Extracts the child text for the current element in the pull parser. 21 * @param parser The XmlPullParser parsing an XML document. 22 * @return The child text for the current element. May be null, if there 23 * is no child text. 24 * @throws XmlPullParserException Thrown if the child text could not be 25 * parsed. 26 * @throws IOException Thrown if the InputStream behind the parser cannot 27 * be read. 28 */ extractChildText(XmlPullParser parser)29 public static String extractChildText(XmlPullParser parser) 30 throws XmlPullParserException, IOException { 31 // TODO: check that the current node is an element? 32 int eventType = parser.next(); 33 if (eventType != XmlPullParser.TEXT) { 34 return null; 35 } 36 return parser.getText(); 37 } 38 39 /** 40 * Extracts the child text for the first child element in the pull parser. 41 * Other child elements will be discarded 42 * @param parser The XmlPullParser parsing an XML document. 43 * @return The child text for the first child element. May be null, if there 44 * is no child text. 45 * @throws XmlPullParserException Thrown if the child text could not be 46 * parsed. 47 * @throws IOException Thrown if the InputStream behind the parser cannot 48 * be read. 49 */ extractFirstChildTextIgnoreRest(XmlPullParser parser)50 public static String extractFirstChildTextIgnoreRest(XmlPullParser parser) 51 throws XmlPullParserException, IOException { 52 int parentDepth = parser.getDepth(); 53 int eventType = parser.next(); 54 String child = null; 55 while (eventType != XmlPullParser.END_DOCUMENT) { 56 int depth = parser.getDepth(); 57 58 if (eventType == XmlPullParser.TEXT) { 59 if (child == null) { 60 child = parser.getText(); 61 } 62 } else if (eventType == XmlPullParser.END_TAG && depth == parentDepth) { 63 return child; 64 } 65 eventType = parser.next(); 66 } 67 throw new XmlPullParserException("End of document reached; never saw expected end tag at " 68 + "depth " + parentDepth); 69 } 70 71 /** 72 * Returns the nextDirectChildTag 73 * @param parser The XmlPullParser parsing an XML document. 74 * @param parentDepth the depth in the hierachy of the parent node 75 * @return The child element 76 * @throws XmlPullParserException Thrown if the child text could not be 77 * parsed. 78 * @throws IOException Thrown if the InputStream behind the parser cannot 79 * be read. 80 */ nextDirectChildTag(XmlPullParser parser, int parentDepth)81 public static String nextDirectChildTag(XmlPullParser parser, int parentDepth) 82 throws XmlPullParserException, IOException { 83 int targetDepth = parentDepth + 1; 84 int eventType = parser.next(); 85 while (eventType != XmlPullParser.END_DOCUMENT) { 86 int depth = parser.getDepth(); 87 88 if (eventType == XmlPullParser.START_TAG && depth == targetDepth) { 89 return parser.getName(); 90 } 91 92 if (eventType == XmlPullParser.END_TAG && depth == parentDepth) { 93 return null; 94 } 95 eventType = parser.next(); 96 } 97 throw new XmlPullParserException("End of document reached; never saw expected end tag at " 98 + "depth " + parentDepth); 99 } 100 } 101