public static void main(String[] args) throws Exception {
.... xmlReader.setContentHandler( new MyContentHandler( ... ) ); .... } class MyContentHandler extends DefaultHandler { public void startDocument(){ ... } public void endDocument(){ ... } public void startElement(String ns, String sname, String qName, Attributes atts) { ... } public void endElement(String ns, String sname, String qName) { ... } public void characters(char[] ch, int start, int length){ ... } } |
The Attributes interface shows how attributes of elements may be accessed.
<?xml version="1.0" encoding="UTF-8"?>
<document title="red" canvas="yellow"> <section> <title>First section</title> <sentence>Some text.</sentence> <sentence>More text.</sentence> </section> <section> <title>Second section</title> <sentence>Another text.</sentence> <sentence>Yet more text.</sentence> </section> </document> |
<rect x="0" y="0" width="100%"
height="15" fill="yellow"/> <text x="10" y="15" stroke="red">First section</text> <rect x="0" y="15" width="100%" height="15" fill="yellow"/> <text x="10" y="30" stroke="black">Some text.</text> |
Note the wasteful approach used for painting the background!