public Object parseAndClose(Reader reader, Type dataType) throws IOException { try { // Initialize the parser XmlPullParser parser = Xml.createParser(); parser.setInput(reader); return readObject(parser, dataType); } catch (XmlPullParserException e) { IOException exception = new IOException(); exception.initCause(e); throw exception; } finally { reader.close(); } } }
/** * Parses content from given reader input stream. */ public void parseXml(Reader reader) throws IOException, XmlPullParserException { this.xmlPullParser.setInput(reader); Xml.parseElement(this.xmlPullParser, this, this.defaultNamespaceDictionary, null); }
/** * Returns a new XML serializer. * * @throws IllegalArgumentException if encountered an {@link XmlPullParserException} */ public static XmlSerializer createSerializer() { try { return getParserFactory().newSerializer(); } catch (XmlPullParserException e) { throw new IllegalArgumentException(e); } }
parseNamespacesForElement(parser, namespaceDictionary); String attributeAlias = attributeNamespace.length() == 0 ? "" : namespaceDictionary.getNamespaceAliasForUriErrorOnUnknown(attributeNamespace); String fieldName = getFieldName(true, attributeAlias, attributeNamespace, attributeName); Field field = classInfo == null ? null : classInfo.getField(fieldName); parseAttributeOrTextContent(parser.getAttributeValue(i), field, valueType, parseAttributeOrTextContent(parser.getText(), field, valueType, parseTextContentForElement(parser, context, true, null); } else { parseNamespacesForElement(parser, namespaceDictionary); String namespace = parser.getNamespace(); String alias = namespaceDictionary.getNamespaceAliasForUriErrorOnUnknown(namespace); String fieldName = getFieldName(false, alias, namespace, parser.getName()); case XmlPullParser.TEXT: if (!ignore && level == 1) { parseAttributeOrTextContent(parser.getText(), field, valueType,
/** * Shows a debug string representation of an element data object of key/value pairs. * * @param element element data object ({@link GenericXml}, {@link Map}, or any object with public * fields) * @param elementName optional XML element local name prefixed by its namespace alias -- for * example {@code "atom:entry"} -- or {@code null} to make up something */ public String toStringOf(String elementName, Object element) { try { StringWriter writer = new StringWriter(); XmlSerializer serializer = Xml.createSerializer(); serializer.setOutput(writer); serialize(serializer, elementName, element, false); return writer.toString(); } catch (IOException e) { throw new IllegalArgumentException(e); } }
/** * Parses an XML element using the given XML pull parser into the given destination object. * * <p> * Requires the the current event be {@link XmlPullParser#START_TAG} (skipping any initial * {@link XmlPullParser#START_DOCUMENT}) of the element being parsed. At normal parsing * completion, the current event will either be {@link XmlPullParser#END_TAG} of the element being * parsed, or the {@link XmlPullParser#START_TAG} of the requested {@code atom:entry}. * </p> * * @param parser XML pull parser * @param destination optional destination object to parser into or {@code null} to ignore XML * content * @param namespaceDictionary XML namespace dictionary to store unknown namespaces * @param customizeParser optional parser customizer or {@code null} for none */ public static void parseElement(XmlPullParser parser, Object destination, XmlNamespaceDictionary namespaceDictionary, CustomizeParser customizeParser) throws IOException, XmlPullParserException { ArrayList<Type> context = new ArrayList<Type>(); if (destination != null) { context.add(destination.getClass()); } parseElementInternal(parser, context, destination, null, namespaceDictionary, customizeParser); }
public final void writeTo(OutputStream out) throws IOException { XmlSerializer serializer = Xml.createSerializer(); serializer.setOutput(out, getCharset().name()); writeTo(serializer); }
/** * Parses content from given reader input stream and namespace dictionary. */ protected void parseXml(Reader reader, XmlNamespaceDictionary namespaceDictionary) throws IOException, XmlPullParserException { this.xmlPullParser.setInput(reader); Xml.parseElement(this.xmlPullParser, this, namespaceDictionary, null); } }
public Object parseAndClose(InputStream in, Charset charset, Type dataType) throws IOException { try { // Initialize the parser XmlPullParser parser = Xml.createParser(); parser.setInput(in, charset.name()); return readObject(parser, dataType); } catch (XmlPullParserException e) { IOException exception = new IOException(); exception.initCause(e); throw exception; } finally { in.close(); } }
/** Returns a new XML pull parser. */ public static XmlPullParser createParser() throws XmlPullParserException { return getParserFactory().newPullParser(); }
/** * Parses content from given reader input stream. */ public void parseXml(Reader reader) throws IOException, XmlPullParserException { this.xmlPullParser.setInput(reader); Xml.parseElement(this.xmlPullParser, this, this.defaultNamespaceDictionary, null); }
/** * Constructs a new XmlEntity class. */ public XmlEntity() throws XmlPullParserException { super.namespaceDictionary = new XmlNamespaceDictionary(); super.namespaceDictionary.set("s3", "http://s3.amazonaws.com/doc/2006-03-01/"); super.namespaceDictionary.set("", ""); this.xmlPullParser = Xml.createParser(); this.defaultNamespaceDictionary = new XmlNamespaceDictionary(); }
/** * Parses content from given reader input stream and namespace dictionary. */ protected void parseXml(Reader reader, XmlNamespaceDictionary namespaceDictionary) throws IOException, XmlPullParserException { this.xmlPullParser.setInput(reader); Xml.parseElement(this.xmlPullParser, this, namespaceDictionary, null); } }
try { Atom.checkContentType(response.getContentType()); XmlPullParser parser = Xml.createParser(); parser.setInput(content, null); AtomFeedParser<T, E> result =
@Override protected Object parseEntryInternal() throws IOException, XmlPullParserException { XmlPullParser parser = this.parser; String kind = parser.getAttributeValue(GoogleAtom.GD_NAMESPACE, "kind"); Class<?> entryClass = this.kindToEntryClassMap.get(kind); if (entryClass == null) { throw new IllegalArgumentException("unrecognized kind: " + kind); } Object result = Types.newInstance(entryClass); Xml.parseElement(parser, result, namespaceDictionary, null); return result; }
try { Atom.checkContentType(response.contentType); XmlPullParser parser = Xml.createParser(); parser.setInput(content, null); MultiKindFeedParser<T> result = new MultiKindFeedParser<T>();
private Object readObject(XmlPullParser parser, Type dataType) throws XmlPullParserException, IOException { Preconditions.checkArgument(dataType instanceof Class<?>, "dataType has to be of Class<?>"); Object result = Types.newInstance((Class<?>) dataType); Xml.parseElement(parser, result, namespaceDictionary, null); return result; }
/** * Constructs a new XmlEntity class. */ public XmlEntity() throws XmlPullParserException { super.namespaceDictionary = new XmlNamespaceDictionary(); super.namespaceDictionary.set("s3", "http://s3.amazonaws.com/doc/2006-03-01/"); super.namespaceDictionary.set("", ""); this.xmlPullParser = Xml.createParser(); this.defaultNamespaceDictionary = new XmlNamespaceDictionary(); }
/** * Parse the next item in the feed and return a new parsed instance of the item type. If there is * no item to parse, it will return {@code null} and automatically close the parser (in which case * there is no need to call {@link #close()}. * * @throws IOException I/O exception * @throws XmlPullParserException XML pull parser exception */ public Object parseNextEntry() throws IOException, XmlPullParserException { if (!feedParsed) { feedParsed = true; Xml.parseElement(parser, null, namespaceDictionary, Atom.StopAtAtomEntry.INSTANCE); } boolean close = true; try { if (parser.getEventType() == XmlPullParser.START_TAG) { Object result = parseEntryInternal(); parser.next(); close = false; return result; } } finally { if (close) { close(); } } return null; }
/** * Parse the feed and return a new parsed instance of the feed type. This method can be skipped if * all you want are the items. * * @throws IOException I/O exception * @throws XmlPullParserException XML pull parser exception */ public T parseFeed() throws IOException, XmlPullParserException { boolean close = true; try { this.feedParsed = true; T result = Types.newInstance(feedClass); Xml.parseElement(parser, result, namespaceDictionary, Atom.StopAtAtomEntry.INSTANCE); close = false; return result; } finally { if (close) { close(); } } }