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

Extending Parsing Process

From all that you have seen until now it's possible to think how to extend the parsing process in two ways:

  1. Changing the methods you want
  2. Introducing a new method in your class

Consider the XML code bellow:

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

<beanDescriptor>

      <line>

            <property  mandatory="true" >

            body of the property

            </property >

      </line>

</beanDescriptor>

Consider that you create one more property, like the highlighted:

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

<beanDescriptor>

      <line>

            <property  mandatory="true" page="5" >

            body of the property

            </property >

      </line>

      <afterLine/>

</beanDescriptor>

Using JColtrane, you create your class to parsing:

public class Parser1 {

      @StartElement(tag="line")

      public void parse(@CurrentBranch String branch){

            System.out.println(branch);

            System.out.println("execute some action");

      }

}

Making a test for it:

public class ParserTest {

      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\\example6\\Example6.xml");

            if(parser!=null){

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

                  try {

                        parser.parse(input,new JColtraneXMLHandler(newParser1()));

                  } catch (SAXException e) {

                        e.printStackTrace();

                  } catch (IOException e) {

                        e.printStackTrace();

                  }

            }

      }

}

If you try to parse the document after the line was included in XML, there will be no changing in the parsing process. To make the class do some action with the new element, we could modify the method:

public class Parser1 {

      @StartElement(tag="line|afterLine")

      public void parse(@CurrentBranch String branch, @Tag String tag){

            System.out.println(branch);

            if(tag.equals("line"))

                  System.out.println("execute some action");

            else

                  System.out.println("execute another action");

      }

}

But this is not a good aproach, once changing the code is hard to maintain.

One better aproach could be do another method in the class:

public class Parser1 {

      @StartElement(tag="line")

      public void parse(@CurrentBranch String branch){

            System.out.println(branch);

            System.out.println("execute some action");

      }

     

      @StartElement(tag="afterLine")

      public void parse2(@CurrentBranch String branch){

            System.out.println(branch);

            System.out.println("execute another action");

      }

}

So, with this aproach, you need just to test if your new method is working.

You could, either, handle the new element in another class:

public class Parser2 {

      @StartElement(tag="afterLine")

      public void parse2(@CurrentBranch String branch){

            System.out.println(branch);

            System.out.println("execute another action");

      }

}

Changing the ParserTest:

public class ParserTest {

      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\\example6\\Example6.xml");

            if(parser!=null){

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

                  try {

                        parser.parse(input,new JColtraneXMLHandler(new Parser1(),new Parser2()));

                  } catch (SAXException e) {

                        e.printStackTrace();

                  } catch (IOException e) {

                        e.printStackTrace();

                  }

            }

      }

}

As you can see, JColtraneXMLHandler accepts more classes in its construtor. So, you can separate your parsing in classes, if you want.

The good side is that you avoid classes with a lot of methods. The bad thing is that the option priority work only with the methods from the same class. So, if two different classes execute some action in the same time, for example in a start element from the element line, you can't now determine the order of execution.

So, choosing which technic is better it's up to you.