XmlParser.java
  1 /*
  2  * XmlParser.java
  3  * @author __Alexander Scheidl__
  4  *
  5  * Created on 07. Februar 2009, 18:38
  6  *
  7  * Parser for XML Files
  8  * with the Restriction that only elementary objects are processed
  9  *
 10  */
 11 
 12 package testxml;
 13 
 14 //import java.util.Vector;  //only needed if vector is used
 15 
 16 
 17 public class XmlParser {
 18     
 19     private String s = ""; // Main-XML String for Parsing
 20 //    String str ="";
 21    
 22     //private Vector v = new Vector(); // not good because if not initialized error returned, which has to be catched
 23     private String[] tab;  // if not set null is returned, better   
 24     
 25     private int counter = 0; // Counter for the elements 
 26     private int i, j; // variable for the indexOf elements
 27     
 28     
 29      
 30     
 31     /** Creates a new instance of XmlParser */
 32     public XmlParser(String s) {
 33         this.s = s;
 34         this.tab = new String[20];
 35     }
 36     
 37     public XmlParser(String s, String element) {
 38         this.s = s;
 39         this.tab = new String[20];
 40         this.setElement(element);
 41     }
 42     
 43     public XmlParser(String s, int size) {
 44         this.s = s;
 45         this.tab = new String[size];
 46     }
 47      
 48     /** 
 49      *  gives you the String within an element (Start- and Endelement)
 50      *  @return String[i] i symbolizes the position of the element 
 51      */
 52     public String getString(int i){
 53         return this.tab[i];
 54         //return (String) this.v.elementAt(i);  //for vector v
 55     }
 56     
 57     /**
 58      *  gives you the number of elements
 59      *  @return int counter
 60      */
 61     public int getNumOfElements(){
 62         return this.counter;       
 63     }
 64     
 65     /**
 66      *  sets an array of Strings between a given element
 67      *  only elementary objects are found
 68      */
 69     public void setElement(String element){
 70         boolean endreached = false; // shows you if end of xml substring in accordance with the element is reached
 71         String reduce = s;    // second variable to do not loose the main string
 72         String startelementn = new String();
 73         String endelementn = new String();
 74         startelementn = "<"+element+">";           // adds < and > to the startvalue of element
 75         endelementn = "</"+element+">";              // adds </ and > to the endvalue of the element
 76 
 77         
 78         while (endreached == false){   // starting a loop to get all elements
 79             i = reduce.indexOf(startelementn) + startelementn.length(); // gives the startpoint for the Substring in the XML String
 80             j = reduce.indexOf(endelementn);                            // gives the endpoint for the Substring in the XML String
 81             
 82 //          System.out.println("i: "+i+" j: "+j); // test purposes only
 83 //          System.out.println(reduce); // test purposes only
 84 //          str = reduce.substring(i,j);
 85             if (j == -1){ // j = -1, if no endelement exists
 86 //          if (str.trim() =="") {
 87                 endreached = true;   //break
 88             }else{
 89                 tab[counter] = reduce.substring(i,j); // puts the substring between start and endelement into an arry                
 90                 //v.add(reduce.substring(i,j));         //if a vector is used
 91 //              System.out.println(tab[counter] );     // test purposes only
 92                 counter++;                              // increase number of found stings by one
 93                 reduce = reduce.substring(j+endelementn.length());  // new substring of the residual elements             
 94             }
 95         
 96         }
 97 
 98     }
 99     
100     
101         
102         
103     
104     
105 
106     
107     
108     
109 }
110