XMLHandler.java (c) (ABA."Saba")
package abagames.gglmekata;

import java.io.*;

/**
 * Read XML file and extract an element.
 */
public class XMLHandler {

  public static String readFileString(String fileName)
   throws FileNotFoundException, UnsupportedEncodingException, IOException {
    StringBuffer strBuf = new StringBuffer();
    BufferedReader reader =
     new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
    for ( ; ; ) {
      String ln = reader.readLine();
      if ( ln == null ) break;
      strBuf.append(ln);
    }
    reader.close();
    return strBuf.toString();
  }

  public static void writeFileString(String fileName, String str)
   throws FileNotFoundException, UnsupportedEncodingException {
    PrintWriter writer =
      new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"));
    writer.print(str);
    writer.close();
  }

  public static String getElement(String xmlStr, String tag) {
    int stIdx = xmlStr.indexOf("<"+tag);
    if ( stIdx == -1 ) return "";
    stIdx = xmlStr.indexOf('>', stIdx) + 1;
    int edIdx = xmlStr.indexOf("</"+tag);
    return xmlStr.substring(stIdx, edIdx);
  }

  public static String getElementWithTag(String xmlStr, String tag) {
    int stIdx = xmlStr.indexOf("<"+tag);
    if ( stIdx == -1 ) return "";
    int edIdx = xmlStr.indexOf("</"+tag);
    edIdx = xmlStr.indexOf('>', edIdx) + 1;
    return xmlStr.substring(stIdx, edIdx);
  }
}