ContentWelcomeProducts Abeona Gaia News Documentation Introduction Swing support Server application Validation XML DOM support Reader Writer Logging Utilities Launcher License Download Hephaistos JavaGantt Services Promote your software Contact Us Links |
ReaderClass eu.beesoft.gaia.xml.XmlReader is usable to read XML document in SAX style. It suppresses the interactivity with W3C (SAX) structures and creates its own structure instead. This structure is called {@link XmlElement} and offers these advantages:
All of this is done to make the XML parsing and processing easier. You don't need to create your own structures for each application you write - the XmlElement meets your needs. There are 3 methods you can / should override in your subclass:
Of course, you can override any of the methods of org.xml.sax.helpers.DefaultHandler, they are accessible, but you will not see it necessary, probably. Let's look at some example. Here is a part of the XML file, with two nested elements and we want to load it and store data from XML to our instances of Person and City class (they can be some plain value objects): <person name="Johny" > <city name="London" /> </person > Here is a our subclass of XmlReader: public class MyXmlProcessor extends XmlReader { private List<Person> persons = new ArrayList<Person> (); public List<Person> getPersons () { return persons; } public void startElement (XmlElement element) { String tag = element.getTag (); if ("person".equals (tag)) { Person p = new Person (); String name = element.getElementAttributes ().get ("name"); p.setName (name); persons.add (p); // store new Person instance to element as an user object element.setUserObject (p); } else if ("city".equals (tag)) { City c = new City (); String name = element.getElementAttributes ().get ("name"); c.setName (name); // here you can see the usage of the user object: Person p = (Person) element.getParentElement ().getUserObject (); p.setCity (c); } } public void endElement (XmlElement element) { // empty } public void characters (XmlElement element, char[] characters, int offset, int length) { // empty } } And here you can see its usage: File xmlFile = new File (...); MyXmlProcessor processor = new MyXmlProcessor (); processor.read (file); List<Person> persons = processor.getPersons ();
|