| XmlParser_2.java |
1 /* 2 * XmlParser_2.java 3 * @author Alexander Scheidl 4 * 5 * 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_2 { 18 19 private String s = ""; // Main-XML String for Parsing 20 21 private String[] tab; // if not set null is returned, better 22 private int counter = 0; // Counter for the elements 23 private int i, j; // variable for the indexOf elements 24 private int itest; // test variable @see setelement(String) for more details 25 26 /** 27 * Creates a new instance of XmlParser_2 28 */ 29 public XmlParser_2(String s) { 30 this.s = s; 31 this.tab = new String[20]; 32 } 33 34 public XmlParser_2(String s, String element) { 35 this.s = s; 36 this.tab = new String[20]; 37 this.setElement(element); 38 } 39 40 public XmlParser_2(String s, int size) { 41 this.s = s; 42 this.tab = new String[size]; 43 } 44 45 /** 46 * gives you the String within an element (Start- and Endelement) 47 * @return String[i] i symbolizes the position of the element 48 */ 49 public String getString(int i){ 50 return this.tab[i]; 51 //return (String) this.v.elementAt(i); //for vector v 52 } 53 54 /** 55 * gives you the number of elements 56 * @return int counter 57 */ 58 public int getNumOfElements(){ 59 return this.counter; 60 } 61 62 /** 63 * sets an array of Strings between a given element<br><br> 64 * 65 * Thist method takes use of the indexOf(Sting) method, so that means if per <br> 66 * example an xml tag with <v> needs to be parsed the start (i) and endposition (j) <br> 67 * between start and endelment <v>content</v> is returned and content is put into an <br> 68 * array. This operations is looped as long as an start and end element is found. 69 * 70 */ 71 public void setElement(String element){ 72 boolean endreached = false; // shows you if end of xml substring in accordance with the element is reached 73 String reduce = s; // second variable to do not loose the main string 74 String test=""; // temp test string 75 String startelementn = new String(); 76 String endelementn = new String(); 77 startelementn = "<"+element+">"; // adds < and > to the startvalue of element 78 endelementn = "</"+element+">"; // adds </ and > to the endvalue of the element 79 80 81 while (endreached == false){ // starting a loop to get all elements 82 if (reduce.indexOf(startelementn) != -1) { // if startelement exists 83 i = reduce.indexOf(startelementn) + startelementn.length(); //gives the startpoint for the Substring in the XML String 84 } else { 85 i = -1; // no startelement 86 } 87 88 j = reduce.indexOf(endelementn); // gives the endpoint for the Substring in the XML String 89 90 91 if (j == -1){ // j = -1, if no endelement exists 92 endreached = true; //break 93 }else{ // if an endelement exists 94 if (reduce.indexOf(startelementn) > j | i == -1){ // if endelement comes before startelement or if no startelement exists 95 96 i = reduce.indexOf(endelementn) + endelementn.length(); // i = end of endelement 97 reduce = reduce.substring(i); // set new reduce string from end of endelement 98 i = 0; // set startingpoint to 0 99 if (reduce.indexOf(startelementn) != 0) { continue; } // if startelement is not at the begin go to begin of loop 100 j = reduce.indexOf(endelementn); // gives you the endpoint of the new string 101 102 if (j == -1) { continue;} // if no endelement exists, go to begin of loop 103 } 104 105 test = reduce.substring(i,j); // initialize new teststring from i to j 106 itest = test.indexOf(startelementn); // test if the teststring contains a startelement 107 108 if (itest == -1){ // if teststring contains no startelement, put it into array 109 tab[counter] = test; // puts the substring between start and endelement into an arry 110 counter++; // increase number of found stings by one 111 reduce = reduce.substring(j+endelementn.length()); // new substring of the residual elements 112 } else { // if teststring contains a startelement 113 114 reduce = reduce.substring(itest+i); // set reducestring from old i + testi 115 116 } 117 118 } 119 120 } 121 122 } 123 124 125 126 127 128 129 130 131 132 133 } 134