Class XmlElement

java.lang.Object
com.codename1.backend.xml.XmlElement

public final class XmlElement extends Object

One element of a small in-memory XML tree, built by XmlPullParser.

XmlElement root = XmlElement.parse(bytes);
for (XmlElement item : root.children("item")) {
    String id = item.getAttribute("id");
    String title = item.child("title").getText();
}

The tree keeps elements, attributes and text; comments, processing instructions and the document type are dropped, exactly as the parser drops them. It carries every limit of the parser, including its refusal to expand any entity a document type declares, so it is safe for untrusted input.

  • Method Details

    • parse

      public static XmlElement parse(byte[] utf8) throws XmlException

      Parses a UTF-8 document and answers its root element.

      Throws
      • XmlException: when the document is not well formed; see XmlPullParser.
      Throws:
      XmlException
    • parse

      public static XmlElement parse(String document) throws XmlException

      Parses a document held in a string and answers its root element.

      Throws
      • XmlException: when the document is not well formed; see XmlPullParser.
      Throws:
      XmlException
    • parse

      public static XmlElement parse(InputStream in) throws IOException

      Reads in to its end as UTF-8 and answers the root element. The stream is not closed.

      Throws
      • IOException: when reading fails, or an XmlException when the document is not well formed.
      Throws:
      IOException
    • parse

      public static XmlElement parse(XmlPullParser parser) throws XmlException

      Builds the tree from a parser positioned before the root element, reading it to the end of the document. Configure the parser first, for example with XmlPullParser.setMaxDepth(int).

      Throws
      • XmlException: when the document is not well formed.
      Throws:
      XmlException
    • getName

      public String getName()
      The element name as written, prefix included.
    • getAttribute

      public String getAttribute(String attributeName)
      The decoded value of the named attribute, or null when there is none.
    • getAttributes

      public Map<String,String> getAttributes()
      Every attribute, in document order. The map is read-only.
    • getChildren

      public List<XmlElement> getChildren()
      The child elements in document order. The list is read-only.
    • child

      public XmlElement child(String childName)
      The first child element named childName, or null when there is none.
    • children

      public List<XmlElement> children(String childName)
      Every child element named childName, in document order; empty when there is none.
    • getText

      public String getText()
      The element's own character data -- every text and CDATA run directly inside it, concatenated, with whitespace as written. Text inside child elements is not included.
    • childText

      public String childText(String childName)
      The text of the first child named childName, trimmed, or null when there is no such child.
    • toString

      public String toString()
      Description copied from class: Object
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class Object