private void parseChildren(SafeXmlPullParser parser, D doc, E element) { boolean done = false; do { N child = null; parser.next(); switch (parser.getCurrentType()) { case TEXT: child = parseText(parser, doc, element); break; case START_ELEMENT: child = parseElement(parser, doc, element); break; case END_ELEMENT: done = true; break; } if (child != null) { doc.insertBefore(element, child, null); } // This is a bit of judgment call. If this happens, the document is // invalid, since the closing tag is missing. By exiting the loop when // parser is out of tokens we're silently repairing the invalid doc. } while (!done && parser.hasNext()); }
/** * @param xmlString * @return parsed string */ public D parse(String xmlString) { SafeXmlPullParser parser; try { parser = XmlParserFactory.buffered(xmlString); } catch (XmlParseException e) { throw new RuntimeException("Cannot parse xml: " + xmlString, e); } // TODO(ohler): This can be an infinite loop. Fix that. while (parser.getCurrentType() != ItemType.START_ELEMENT) { parser.next(); } D document = factory.create(parser.getTagName(), CollectionUtils.newJavaMap(parser.getAttributes())); parseChildren(parser, document, document.getDocumentElement()); return document; }