Class XmlParser

java.lang.Object
io.kubfly.sdk.codec.xml.XmlParser

public final class XmlParser extends Object
A streaming parser of XML, strict and without a tree: next() reads the document event by event (XmlEvent), a reader takes what it needs and skips the rest.
XmlParser xml = XmlParser.of(body);
xml.next();                                       // START ListBucketResult
while (xml.next() == XmlEvent.START) {
    switch (xml.name()) {
        case "Contents" -> contents(xml);
        case "IsTruncated" -> truncated = xml.elementText().equals("true");
        default -> xml.skip();
    }
}
It takes a subset of XML 1.0 — what servers answer with: elements, attributes, text, the five predefined entities and character references, CDATA sections; the XML declaration (version 1.0, UTF-8) and comments are read past. A document type and processing instructions are failures: there are no entities of the document, so no external entity and no expansion of entities can ever be read (XXE, "billion laughs"). The text is UTF-8, checked strictly; line ends become \n, white space in attribute values a space, as XML says. Names are not split by namespaces: s3:Key is a name; xmlns is an attribute. Every failure — not XML of this subset, beyond the Limits — is a CodecException with the offset; the parser is not used after it. Not thread-safe.
  • Method Details

    • of

      public static XmlParser of(byte[] document)
      A parser of a document with the limits by default (Limits.DEFAULT).
      Parameters:
      document - the document, UTF-8; not copied, not to be changed while it is read
      Returns:
      the parser, before the first event
      Throws:
      CodecException - if the document is longer than the limit
    • of

      public static XmlParser of(byte[] document, Limits limits)
      A parser of a document with limits.
      Parameters:
      document - the document, UTF-8; not copied, not to be changed while it is read
      limits - the limits
      Returns:
      the parser, before the first event
      Throws:
      CodecException - if the document is longer than the limit
    • next

      public XmlEvent next()
      Reads the next event: the first is the XmlEvent.START of the root element, the last XmlEvent.END_DOCUMENT, given again on every call after it.
      Returns:
      the event
      Throws:
      CodecException - if the document is not XML of the subset or is beyond the limits
    • event

      public XmlEvent event()
      The event read last.
      Returns:
      the event, null before the first
    • name

      public String name()
      The name of the element of the XmlEvent.START or the XmlEvent.END read last.
      Returns:
      the name
    • text

      public String text()
      The text of the XmlEvent.TEXT read last.
      Returns:
      the text
    • depth

      public int depth()
      The depth of the event read last: of a XmlEvent.START, with its element (the root is 1); of an XmlEvent.END, without it.
      Returns:
      the depth
    • offset

      public long offset()
      The offset of the event read last: the < of its tag, the first byte of its text.
      Returns:
      the offset in bytes
    • attributeCount

      public int attributeCount()
      The attributes of the XmlEvent.START read last.
      Returns:
      the count
    • attributeName

      public String attributeName(int index)
      The name of an attribute of the XmlEvent.START read last, in the order of the document.
      Parameters:
      index - the index, from 0
      Returns:
      the name
    • attributeValue

      public String attributeValue(int index)
      The value of an attribute of the XmlEvent.START read last, in the order of the document.
      Parameters:
      index - the index, from 0
      Returns:
      the value
    • attribute

      public String attribute(String attribute)
      The value of an attribute of the XmlEvent.START read last, by its name.
      Parameters:
      attribute - the name
      Returns:
      the value, or null if there is none
    • elementText

      public String elementText()
      Reads the text of the element of the XmlEvent.START read last, up to its XmlEvent.END.
      Returns:
      the text, "" for an element without one
      Throws:
      CodecException - if the element has an element inside, or the document fails
      IllegalStateException - if the event read last is not a start
    • skip

      public void skip()
      Reads past the element of the XmlEvent.START read last, up to its XmlEvent.END.
      Throws:
      CodecException - if the document fails
      IllegalStateException - if the event read last is not a start