Code example for Node

Methods: getFirstChild, getTextContent, hasChildNodes

0
 
    public String getText() {
      if (currentNode == null) {
        return ""; 
      } 
      return currentNode.getTextContent();
    } 
 
    public int getLineNumber() { 
      // TODO(msama): The current implementation is 
      //   unable to return line numbers. 
      return -1; 
    } 
 
    public int getEventType() 
        throws XmlPullParserException { 
      return mEventType;
    } 
 
    /*package*/ 
    public boolean isWhitespace(String text)
        throws XmlPullParserException { 
      if (text == null) {
        return false; 
      } 
      return text.split("\\s").length == 0;
    } 
 
    public boolean isWhitespace() 
        throws XmlPullParserException { 
      // Note: in android whitespaces are automatically stripped. 
      // Here we have to skip them manually 
      return isWhitespace(getText()); 
    } 
 
    public String getPrefix() {
      throw new RuntimeException("getPrefix not supported");
    } 
 
    public char[] getTextCharacters(int[] holderForStartAndLength) {
      String txt = getText();
      char[] chars = null;
      if (txt != null) {
        holderForStartAndLength[0] = 0;
        holderForStartAndLength[1] = txt.length();
        chars = new char[txt.length()];
        txt.getChars(0, txt.length(), chars, 0);
      } 
      return chars;
    } 
 
    public String getNamespace() {
      if (currentNode == null) {
        return ""; 
      } 
      String namespace = currentNode.getNamespaceURI();
      if (namespace == null) {
        return ""; 
      } 
      return namespace;
    } 
 
    public String getName() {
      if (currentNode == null) {
        return ""; 
      } 
      return currentNode.getNodeName();
    } 
 
    Node getAttributeAt(int index) {
      if (currentNode == null) {
        throw new IndexOutOfBoundsException(String.valueOf(index));
      } 
      NamedNodeMap map = currentNode.getAttributes();
      if (index >= map.getLength()) {
        throw new IndexOutOfBoundsException(String.valueOf(index));
      } 
      return map.item(index);
    } 
 
    public String getAttribute(String namespace, String name) {
      if (currentNode == null) {
        return null; 
      } 
 
      Element element = (Element) currentNode;
      if (element.hasAttributeNS(namespace, name)) {
        return element.getAttributeNS(namespace, name);
      } 
      return null; 
    } 
 
    public String getAttributeNamespace(int index) {
      Node attr = getAttributeAt(index);
      if (attr == null) {
        return null; 
      } 
      return attr.getNamespaceURI();
    } 
 
    public String getAttributeName(int index) {
      try { 
        Node attr = getAttributeAt(index);
        return attr.getNodeName();
      } catch (IndexOutOfBoundsException ex) {
        return null; 
      } 
    } 
 
    public String getAttributePrefix(int index) {
      throw new RuntimeException("getAttributePrefix not supported");
    } 
 
    public boolean isEmptyElementTag() throws XmlPullParserException { 
      // In Android this method is left unimplemented. 
      // This implementation is mirroring that. 
      return false; 
    } 
 
    public int getAttributeCount() { 
      if (currentNode == null) {
        return -1; 
      } 
      return currentNode.getAttributes().getLength();
    } 
 
    public String getAttributeValue(int index) {
      return qualify(getAttributeAt(index).getNodeValue());
    } 
 
    private String qualify(String value) {
      Attribute attribute = asAttribute(value);
      if (attribute == null) return null;
      return attribute.qualifiedValue();
    } 
 
    private Attribute asAttribute(String value) {
      if (value == null) return null;
      return new Attribute(FAKE_RES_NAME, value, packageName);
    } 
 
    public String getAttributeType(int index) {
      // Android always returns CDATA even if the 
      // node has no attribute. 
      return "CDATA"; 
    } 
 
    public boolean isAttributeDefault(int index) {
      // The android implementation always returns false 
      return false; 
    } 
 
    public int nextToken() throws XmlPullParserException, IOException { 
      return next(); 
    } 
 
    public String getAttributeValue(String namespace, String name) {
      return qualify(getAttribute(namespace, name));
    } 
 
    public int next() throws XmlPullParserException, IOException { 
      if (!mStarted) {
        mStarted = true;
        return START_DOCUMENT;
      } 
      if (mEventType == END_DOCUMENT) {
        return END_DOCUMENT;
      } 
      int ev = nativeNext();
      if (mDecNextDepth) {
        mDepth--;
        mDecNextDepth = false;
      } 
      switch (ev) {
        case START_TAG:
          mDepth++;
          break; 
        case END_TAG:
          mDecNextDepth = true;
          break; 
      } 
      mEventType = ev;
      if (ev == END_DOCUMENT) {
        // Automatically close the parse when we reach the end of 
        // a document, since the standard XmlPullParser interface 
        // doesn't have such an API so most clients will leave us 
        // dangling. 
        close(); 
      } 
      return ev;
    } 
 
    /** 
     * A twin implementation of the native android nativeNext(status) 
     * 
     * @throws XmlPullParserException 
     */ 
    private int nativeNext() throws XmlPullParserException { 
      switch (mEventType) {
        case (CDSECT): {
          throw new IllegalArgumentException(
              "CDSECT is not handled by Android"); 
        } 
        case (COMMENT): {
          throw new IllegalArgumentException(
              "COMMENT is not handled by Android"); 
        } 
        case (DOCDECL): {
          throw new IllegalArgumentException(
              "DOCDECL is not handled by Android"); 
        } 
        case (ENTITY_REF): {
          throw new IllegalArgumentException(
              "ENTITY_REF is not handled by Android"); 
        } 
        case (END_DOCUMENT): {
          // The end document event should have been filtered 
          // from the invoker. This should never happen. 
          throw new IllegalArgumentException(
              "END_DOCUMENT should not be found here."); 
        } 
        case (END_TAG): {
          return navigateToNextNode(currentNode);
        } 
        case (IGNORABLE_WHITESPACE): {
          throw new IllegalArgumentException(
              "IGNORABLE_WHITESPACE"); 
        } 
        case (PROCESSING_INSTRUCTION): {
          throw new IllegalArgumentException(
              "PROCESSING_INSTRUCTION"); 
        } 
        case (START_DOCUMENT): {
          currentNode = document.getDocumentElement();
          return START_TAG;
        } 
        case (START_TAG): {
          if (currentNode.hasChildNodes()) {
            // The node has children, navigate down 
            return processNextNodeType( 
                currentNode.getFirstChild());
          } else { 
            // The node has no children 
            return END_TAG;
          } 
        }