Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class XMLParser
- Object
- XMLParser
Known subtypesHTMLParser
Parser class used to parse an XML document into a DOM object (Element). This code was originally developed to parse HTML and as a result isn’t as strict as most XML parsers and can parse many HTML documents out of the box. The parser is mostly stateful (although it does have an event callback API as well), its modeled closely to the Java DOM API’s.
In this sample an XML hierarchy is displayed using a com.codename1.ui.tree.Tree:
class XMLTreeModel implements TreeModel {
private Element root;
public XMLTreeModel(Element e) {
root = e;
}
public Vector getChildren(Object parent) {
if(parent == null) {
Vector c = new Vector();
c.addElement(root);
return c;
}
Vector result = new Vector();
Element e = (Element)parent;
for(int iter = 0 ; iter
Constructors
public XMLParser() |
Methods
protected String getSupportedStandardName() | Returns a string identifying the document type this parser supports. |
public void addCharEntity(String symbol, int code) | Adds the given symbol and code to the user defined char entities table http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references |
public void addCharEntitiesRange(String[] symbols, int startcode) | Adds the given symbols array to the user defined char entities table with the startcode provided as the code of the first string, startcode+1 for the second etc. Some strings in the symbols array may be null thus skipping code numbers. |
protected String convertCharEntity(String charEntity) | Converts a char entity to the matching character. |
public Element parse(Reader is) | This is the entry point for parsing a document and the only non-private member method in this class |
protected Element createNewElement(String name) | Creates a new element. |
protected Element createNewTextElement(String text) | Creates a new text element. |
public void setIncludeWhitespacesBetweenTags(boolean include) | |
public void eventParser(Reader r)
throws IOException | The event parser requires deriving this class and overriding callback methods to work effectively. |
protected void textElement(String text) | Invoked when the event parser encounters a text element. |
protected boolean startTag(String tag) | Invoked when a tag is opened, this method should return true to process the tag or return false to skip the tag. |
protected void endTag(String tag) | Invoked when a tag ends This callback method is invoked only on the eventParser. |
protected void attribute(String tag, String attributeName, String value) | Invoked for every attribute value of the givne tag This callback method is invoked only on the eventParser. |
protected void parseTagContent(Element element, Reader is)
throws IOException | Parses tags content, accumulating text and child elements . Upon bumping a start tag character it calls the parseTag method. |
protected boolean isWhiteSpace(char ch) | Checks if the specified character is a white space or not. |
protected Element parseTag(Reader is)
throws IOException | This method collects the tag name and all of its attributes. |
protected Element parseCommentOrXMLDeclaration(Reader is, String endTag)
throws IOException | This utility method is used to parse comments and XML declarations in the XML. |
protected boolean isEmptyTag(String tagName) | Checks whether the specified tag is an empty tag |
protected void notifyError(int errorId, String tag, String attribute, String value, String description) | A utility method used to notify an error to the ParserCallback and throw an IllegalArgumentException if parsingError returned false |
protected boolean isSupported(Element element) | Returns true if this element is supported, false otherwise In XMLParser this always returns true, but subclasses can determine if an element is supported in their context according to its name etc. Unsupported elements will be skipped by th… |
protected boolean shouldEvaluate(Element element) | Checks if this element should be evaluated by the parser This can be overriden by subclasses to skip certain elements |
public void setParserCallback(ParserCallback parserCallback) | Sets the specified callback to serve as the callback for parsing errors |
public boolean isCaseSensitive() | Sets the parser to be case sensitive and retain case, otherwise it will convert all data to lower case |
public void setCaseSensitive(boolean caseSensitive) | Sets the parser to be case sensitive and retain case, otherwise it will convert all data to lower case |
public void setStrict(boolean strict) | Turns strict XML parsing on or off (off by default). |
public boolean isStrict() | Returns true if strict XML parsing is on, see setStrict(boolean). |
public void setStripNamespacePrefixes(boolean strip) | When on, the namespace prefix is removed from tag and attribute names as they are read, so w:p is reported as p and w:val as val. |
public boolean isStripNamespacePrefixes() | Returns true if namespace prefixes are removed from names, see setStripNamespacePrefixes(boolean). |
Inherited methods
Constructor details
XMLParser
public XMLParser()Method details
getSupportedStandardName
protected String getSupportedStandardName()Returns
addCharEntity
public void addCharEntity(String symbol, int code)Parameters
symbolString- The symbol to add
codeint- The symbol’s code
addCharEntitiesRange
public void addCharEntitiesRange(String[] symbols, int startcode)Parameters
symbolsString[]- The symbols to add
startcodeint- The symbol’s code
convertCharEntity
protected String convertCharEntity(String charEntity)Parameters
charEntityString- The char entity to convert
Returns
parse
public Element parse(Reader is)Parameters
isReader- The InputStream containing the XML
Returns
createNewElement
protected Element createNewElement(String name)Parameters
nameString- The new element’s name
Returns
createNewTextElement
protected Element createNewTextElement(String text)Parameters
textString- The new element’s text
Returns
setIncludeWhitespacesBetweenTags
public void setIncludeWhitespacesBetweenTags(boolean include)eventParser
public void eventParser(Reader r)
throws IOExceptionParameters
rReader- the reader from which the data should be parsed
Throws
java.io.IOException- if an exception is thrown by the reader
textElement
protected void textElement(String text)Parameters
textString- the text encountered
startTag
protected boolean startTag(String tag)Parameters
tagString- the tag name
Returns
endTag
protected void endTag(String tag)Parameters
tagString- the tag name
attribute
protected void attribute(String tag, String attributeName, String value)Parameters
tagString- the tag name
attributeNameString- Not documented.
valueString- Not documented.
parseTagContent
protected void parseTagContent(Element element, Reader is)
throws IOExceptionParameters
elementElement- The current parent element
isReader- The InputStream containing the XML
Throws
IOException- if an I/O error in the stream is encountered
isWhiteSpace
protected boolean isWhiteSpace(char ch)Parameters
chchar- The character to check
Returns
parseTag
protected Element parseTag(Reader is)
throws IOExceptionParameters
isReader- The InputStream containing the XML
Returns
Throws
IOException- if an I/O error in the stream is encountered
parseCommentOrXMLDeclaration
protected Element parseCommentOrXMLDeclaration(Reader is, String endTag)
throws IOExceptionParameters
isReader- The inputstream
endTagString- The endtag to look for
Returns
Throws
isEmptyTag
protected boolean isEmptyTag(String tagName)Parameters
tagNameString- The tag name to check
Returns
notifyError
protected void notifyError(int errorId, String tag, String attribute, String value, String description)Parameters
errorIdint- The error ID, one of the ERROR_* constants in ParserCallback
tagString- The tag in which the error occured (Can be null for non-tag related errors)
attributeString- The attribute in which the error occured (Can be null for non-attribute related errors)
valueString- The value in which the error occured (Can be null for non-value related errors)
descriptionString- A verbal description of the error
Throws
IllegalArgumentException- If the parser callback returned false on this error
isSupported
protected boolean isSupported(Element element)Parameters
elementElement- The element to check
Returns
shouldEvaluate
protected boolean shouldEvaluate(Element element)Parameters
elementElement- The element to check
Returns
setParserCallback
public void setParserCallback(ParserCallback parserCallback)Parameters
parserCallbackParserCallback- The callback to use for parsing errors
isCaseSensitive
public boolean isCaseSensitive()Returns
setCaseSensitive
public void setCaseSensitive(boolean caseSensitive)Parameters
caseSensitiveboolean- the caseSensitive to set
setStrict
public void setStrict(boolean strict)Turns strict XML parsing on or off (off by default). The default mode keeps the lenient, HTML-flavoured behaviour existing code relies on. Strict mode is for well-formed XML such as OOXML, and differs in that:
names keep their case and are matched exactly, both by the parser (closing tags) and by
Element.getAttribute, as ifsetCaseSensitive(true)were set;text is kept exactly as written: leading whitespace is not collapsed and whitespace-only text between tags is kept as a text element, so
<w:t xml:space="preserve"> </w:t>keeps its space;whitespace is the four XML whitespace characters and
isWhiteSpaceis not consulted.
Parameters
strictboolean- true for strict XML parsing
isStrict
public boolean isStrict()setStrict(boolean).Returns
setStripNamespacePrefixes
public void setStripNamespacePrefixes(boolean strip)w:p is reported as p and w:val as val. Off by
default because it discards information: w:t and m:t both become
t. Namespace declarations (xmlns:w) keep their name. Works in
either mode.Parameters
stripboolean- true to remove namespace prefixes from names
isStripNamespacePrefixes
public boolean isStripNamespacePrefixes()setStripNamespacePrefixes(boolean).