Class XmlParser
java.lang.Object
io.kubfly.sdk.codec.xml.XmlParser
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 Summary
Modifier and TypeMethodDescriptionThe value of an attribute of theXmlEvent.STARTread last, by its name.intThe attributes of theXmlEvent.STARTread last.attributeName(int index) The name of an attribute of theXmlEvent.STARTread last, in the order of the document.attributeValue(int index) The value of an attribute of theXmlEvent.STARTread last, in the order of the document.intdepth()The depth of the event read last: of aXmlEvent.START, with its element (the root is 1); of anXmlEvent.END, without it.Reads the text of the element of theXmlEvent.STARTread last, up to itsXmlEvent.END.event()The event read last.name()The name of the element of theXmlEvent.STARTor theXmlEvent.ENDread last.next()Reads the next event: the first is theXmlEvent.STARTof the root element, the lastXmlEvent.END_DOCUMENT, given again on every call after it.static XmlParserof(byte[] document) A parser of a document with the limits by default (Limits.DEFAULT).static XmlParserA parser of a document with limits.longoffset()The offset of the event read last: the<of its tag, the first byte of its text.voidskip()Reads past the element of theXmlEvent.STARTread last, up to itsXmlEvent.END.text()The text of theXmlEvent.TEXTread last.
-
Method Details
-
of
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
A parser of a document with limits.- Parameters:
document- the document, UTF-8; not copied, not to be changed while it is readlimits- the limits- Returns:
- the parser, before the first event
- Throws:
CodecException- if the document is longer than the limit
-
next
Reads the next event: the first is theXmlEvent.STARTof the root element, the lastXmlEvent.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
-
name
The name of the element of theXmlEvent.STARTor theXmlEvent.ENDread last.- Returns:
- the name
-
text
-
depth
public int depth()The depth of the event read last: of aXmlEvent.START, with its element (the root is 1); of anXmlEvent.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
-
attributeName
The name of an attribute of theXmlEvent.STARTread last, in the order of the document.- Parameters:
index- the index, from 0- Returns:
- the name
-
attributeValue
The value of an attribute of theXmlEvent.STARTread last, in the order of the document.- Parameters:
index- the index, from 0- Returns:
- the value
-
attribute
The value of an attribute of theXmlEvent.STARTread last, by its name.- Parameters:
attribute- the name- Returns:
- the value, or
nullif there is none
-
elementText
Reads the text of the element of theXmlEvent.STARTread last, up to itsXmlEvent.END.- Returns:
- the text,
""for an element without one - Throws:
CodecException- if the element has an element inside, or the document failsIllegalStateException- if the event read last is not a start
-
skip
public void skip()Reads past the element of theXmlEvent.STARTread last, up to itsXmlEvent.END.- Throws:
CodecException- if the document failsIllegalStateException- if the event read last is not a start
-