Download - Forum

User Guide Content

  1. Necessary Previous Knowledge
  2. Classic "Hello World" with SAX
  3. Classic "Hello World" with JColtrane
  4. Basic Annotation Conditions
  5. Filtering Element Options
  6. More Filtering Options
  7. Making Your Own Filter Conditions
  8. Receiving Parameters from JColtrane
  9. Extending Parsing Process

Help this project

The Classic "Hello World" With SAX

Through this guide we will use several examples. All of them can be finded with the framework's download, in folder examples.

We are gonna begin with the classic Hello World example. To ilustrate the XML parsing, it will be used the following XML document:

<?xml version="1.0" encoding="UTF-8"?>

<beanDescriptor>

      <line>

            <property  mandatory="true" >

            body of the property

            </property >

      </line>

</beanDescriptor>

In SAX aproach, you would have to extend DefaultHandler, something like the following class:

public class SAXHandler extends DefaultHandler {

      private StringBuilder currentBranch=new StringBuilder("/");

      private StringBuilder body= new StringBuilder();

     

      public void startDocument(){

            System.out.println("Starting Document: Hello World!!!\n");

      }

     

      public void endDocument(){

            System.out.println("End Document: Bye bye World!!!\n");

      }

     

      public void startElement(String uri,String localName,String tag,Attributes atributos)throws SAXException{

            currentBranch.append(tag+"/");

            System.out.println(currentBranch.toString()); 

            //user action

            System.out.println("Executing some action in start element\n");

            body.delete(0,body.length());

      }

     

      public void endElement(String uri,String localName,String tag)throws SAXException{

            if(!body.toString().equals("\n")&&body.length()!=2)

                  System.out.println(body.toString().trim());

            System.out.println(currentBranch);

            body.delete(0, body.length());

            //user action

            System.out.println("Executing some action in end element\n");

            currentBranch.delete(currentBranch.length()-tag.length()-1, currentBrunch.length());

      }

     

      public void characters(char[] ch, int start, int lenght)throws SAXException{

            body.append(ch,start,lenght);

      }

}

From the above class, we can make some observations:

  • You must override methods defined in Defautl Handler class.
  • In startElement and endElement methods you must to receive some parameters, even if you don't use it all.
  • If you want to save some information about XML, you must to implement your own way to do this (in the case, it was used StringBuilder objects to keep the current branch and body from XML document).

After all the implementation, you could create a class to print the result of parsing, to comprove that your parsing is correct:

public class SAXHandlerTest {

      public static void main(String[] args) {

            SAXParser parser=null;

            try {

                  parser= SAXParserFactory.newInstance().newSAXParser();

            } catch (ParserConfigurationException e) {

                  e.printStackTrace();

            } catch (SAXException e) {

                  e.printStackTrace();

            }

 

            File file=new File("examples\\example1\\Example1.xml");

            if(parser!=null){

                  InputSource input=new InputSource(file.getAbsolutePath());

                  try {

                        parser.parse(input,new SAXHandler());

                  } catch (SAXException e) {

                        e.printStackTrace();

                  } catch (IOException e) {

                        e.printStackTrace();

                  }

            }

      }

}

Running the above class, system presents the following result:

Starting Document: Hello World!!!

/beanDescriptor/
Executing some action in start element

/beanDescriptor/line/
Executing some action in start element

/beanDescriptor/line/property/
Executing some action in start element

body of the property
/beanDescriptor/line/property/
Executing some action in end element

/beanDescriptor/line/
Executing some action in end element

/beanDescriptor/
Executing some action in end element

End Document: Bye bye World!!!

Of course you could encapsulate your desired actions in your own custom classes, making an dependency injection in DefaultHandler with them. But even doing this, you would need to receive some parameter from Default handler and making code to treat data in the way you want. And if you need past information, like current branch in our example, you must to implement it in your way.

Let's try to do the same parsing using JColtrane in the next section Classic Hello World with JColtrane.