Class XmlPullParser
java.lang.Object
com.codename1.backend.xml.XmlPullParser
A small streaming XML parser: call next() until it answers
END_DOCUMENT, reading the element name, attributes and text of each event
as it goes.
XmlPullParser p = XmlPullParser.of(bytes);
for (int ev = p.next(); ev != XmlPullParser.END_DOCUMENT; ev = p.next()) {
if (ev == XmlPullParser.START_ELEMENT && "item".equals(p.getName())) {
String id = p.getAttributeValue("id");
}
}
What it does:
- Elements, attributes (single or double quoted) and text. An empty element
(
<a/>) is reported as a start followed immediately by its end. - Text and CDATA sections are reported together as one
TEXTevent per run of character data between two tags; whitespace is kept as written. Text outside the root element is ignored. - The five predefined entities (
&<>"') and numeric character references (A,A) are decoded, in text and in attribute values. - Namespaces are not resolved: a name is reported as written, prefix included
(
plist:dict,xmlns:a). - Comments, processing instructions and the XML declaration are skipped.
What it refuses, by construction rather than by configuration:
- A document type declaration is skipped, never interpreted. Its internal
subset is not read, so an entity it declares is never defined, an external
one is never fetched, and a reference to either is an
XmlException. That makes the parser immune to external entity (XXE) and entity expansion ("billion laughs") attacks: there is nothing to expand. - Elements nested deeper than
setMaxDepth(int)(256 by default) are rejected, so a hostile document cannot make a recursive consumer such asXmlElementoverflow its stack. - A document that is not well formed -- a mismatched end tag, an unclosed
element, a second root element, a stray
<-- is anXmlException.
Input is decoded as UTF-8; a leading byte order mark is skipped. The
encoding of the XML declaration is not consulted.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe end of the document.static final intAn element's end tag;getName()names the element being closed.static final intAn element's start tag;getName()and the attribute accessors describe it.static final intCharacter data, with entities decoded; read it withgetText(). -
Method Summary
Modifier and TypeMethodDescriptionintThe number of attributes on the current start element; 0 for any other event.getAttributeName(int index) The name of attributeindexof the current start element, as written.getAttributeValue(int index) The decoded value of attributeindexof the current start element.getAttributeValue(String attributeName) The decoded value of the attribute namedattributeName(prefix included) on the current start element, or null when it has none.intgetDepth()The number of elements currently open: 1 inside the root element, 0 before and after it.intThe current event: one ofSTART_ELEMENT,END_ELEMENT,TEXTorEND_DOCUMENT, or 0 before the first call tonext().getName()The element name of the current start or end event, prefix included, or null for any other event.getText()The decoded character data of the currentTEXTevent, or null for any other event.intnext()Advances to the next event and answers its type.static XmlPullParserof(byte[] utf8) A parser over UTF-8 encoded bytes.static XmlPullParserof(byte[] data, int offset, int length) A parser overlengthUTF-8 encoded bytes ofdatastarting atoffset.static XmlPullParserof(InputStream in) A parser over the whole ofin, read as UTF-8.static XmlPullParserA parser over a document that is already a string.setMaxDepth(int maxDepth) Sets the deepest element nesting accepted; deeper is anXmlException.
-
Field Details
-
START_ELEMENT
public static final int START_ELEMENTAn element's start tag;getName()and the attribute accessors describe it.- See Also:
-
END_ELEMENT
public static final int END_ELEMENTAn element's end tag;getName()names the element being closed.- See Also:
-
TEXT
public static final int TEXTCharacter data, with entities decoded; read it withgetText().- See Also:
-
END_DOCUMENT
public static final int END_DOCUMENTThe end of the document. Every later call tonext()answers this again.- See Also:
-
-
Method Details
-
of
A parser over a document that is already a string. -
of
A parser over UTF-8 encoded bytes. -
of
A parser overlengthUTF-8 encoded bytes ofdatastarting atoffset. -
of
A parser over the whole ofin, read as UTF-8. The stream is read to its end but not closed.- Throws:
IOException
-
setMaxDepth
Sets the deepest element nesting accepted; deeper is anXmlException. Answers this parser. -
getEventType
public int getEventType()The current event: one ofSTART_ELEMENT,END_ELEMENT,TEXTorEND_DOCUMENT, or 0 before the first call tonext(). -
getName
The element name of the current start or end event, prefix included, or null for any other event. -
getText
-
getDepth
public int getDepth()The number of elements currently open: 1 inside the root element, 0 before and after it. After anEND_ELEMENTevent the closed element no longer counts. -
getAttributeCount
public int getAttributeCount()The number of attributes on the current start element; 0 for any other event. -
getAttributeName
The name of attributeindexof the current start element, as written. -
getAttributeValue
The decoded value of attributeindexof the current start element. -
getAttributeValue
-
next
Advances to the next event and answers its type.
Throws
XmlException: when the document is not well formed, nests too deeply, or references an entity this parser does not define.
- Throws:
XmlException
-