/** * Returns a child XML element. If a child element is not found, returns an empty element instead * of null. * * @param name name of child element * @return an element */ public XmlElement findChild(String name) { for (final XmlElement child : mChildElements) { if (child.getTagName().equals(name)) { return child; } } return NULL_ELEMENT; }
/** * Returns a list of child elements. If there is no child element, returns a empty list instead of * null. * * @param name name of child element * @return a list of child elements */ public List<XmlElement> findChildren(String name) { final List<XmlElement> tagItemList = new ArrayList<XmlElement>(); for (final XmlElement child : mChildElements) { if (child.getTagName().equals(name)) { tagItemList.add(child); } } return tagItemList; }
/** * Parses XML data and returns the root element. * * @param xmlPullParser parser * @return root element */ public static XmlElement parse(XmlPullParser xmlPullParser) { XmlElement rootElement = XmlElement.NULL_ELEMENT; try { XmlElement parsingElement = XmlElement.NULL_ELEMENT; MAINLOOP: while (true) { switch (xmlPullParser.next()) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: final XmlElement childItem = new XmlElement(); childItem.setTagName(xmlPullParser.getName()); if (parsingElement == XmlElement.NULL_ELEMENT) { rootElement = childItem; } else { parsingElement.putChild(childItem); } parsingElement = childItem; // Set Attribute for (int i = 0; i < xmlPullParser.getAttributeCount(); i++) { parsingElement.putAttribute( xmlPullParser.getAttributeName(i), xmlPullParser.getAttributeValue(i)); } break; case XmlPullParser.TEXT: parsingElement.setValue(xmlPullParser.getText()); break; case XmlPullParser.END_TAG: parsingElement = parsingElement.getParent(); break; case XmlPullParser.END_DOCUMENT: break MAINLOOP; default: break MAINLOOP; } } } catch (final XmlPullParserException e) { Log.e(TAG, "parseXml: XmlPullParserException."); rootElement = XmlElement.NULL_ELEMENT; } catch (final IOException e) { Log.e(TAG, "parseXml: IOException."); rootElement = XmlElement.NULL_ELEMENT; } return rootElement; }
private void putChild(XmlElement childItem) { mChildElements.add(childItem); childItem.setParent(this); }