public class XMLParser extends Object
XMLParser
tokenizes XML
into a series of tags and strings, which are passed
in series to a consumer. The parser does not support CDATA
sections, entities, or comments.
Here is a trivial example that recognizes a few HTML tags read from standard input:
class HTMLParser implements XMLConsumer { XMLParser parser; HTMLParser() { parser = new XMLParser(new InputStreamReader(System.in), this); } void parse() { try { parser.parse(); } catch(IOException ex) { System.out.println("End of input, or other error."); } } public void consumeText(String text) { System.out.println("Text: " + text); } public void consumeElement(XMLElement e) { String tag = e.getTag(); if(tag.equalsIgnoreCase("b")) System.out.println("Bold " + (tag.isEnd() ? "end" : "begin")); else if(tag.equalsIgnoreCase("center")) System.out.println("Centering " + (tag.isEnd() ? "end" : "begin")); } }
Modifier and Type | Field and Description |
---|---|
private XMLConsumer |
consumer |
private boolean |
pre |
private Reader |
reader |
private StreamTokenizer |
st |
private static int |
STATE_COMMENT |
private static int |
STATE_EQUALS |
private static int |
STATE_NAME |
private static int |
STATE_NONE |
private static int |
STATE_TAG |
private static int |
STATE_VALUE |
private StringBuffer |
text |
Constructor and Description |
---|
XMLParser(Reader reader,
XMLConsumer consumer)
Construct a new
XMLParser . |
Modifier and Type | Method and Description |
---|---|
private void |
_whitespaceOff() |
private void |
_whitespaceOn() |
private void |
flushText() |
void |
parse()
Parse the input.
|
private void |
sendElement(XMLElement e) |
private XMLConsumer consumer
private StreamTokenizer st
private boolean pre
private StringBuffer text
private static final int STATE_NONE
private static final int STATE_TAG
private static final int STATE_NAME
private static final int STATE_EQUALS
private static final int STATE_VALUE
private static final int STATE_COMMENT
public XMLParser(Reader reader, XMLConsumer consumer)
XMLParser
.reader
- The reader to parse input from.consumer
- The XMLConsumer
that will consume text
and XML elements decoded from the reader.public void parse() throws IOException
IOException
- If an error occurs on the input stream.private void flushText()
private void sendElement(XMLElement e)
private void _whitespaceOff()
private void _whitespaceOn()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses