Source Code : Parse Properties Files

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Parse Properties Files

Parse Properties Files
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.Properties;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.Properties;
import java.util.Enumeration;
import org.apache.xerces.parsers.*;

public class ParseNonXML extends DefaultHandler {

  public static void main(String args[]) throws SAXException {

    PropertyFileParser pfp = new PropertyFileParser();
    pfp.setContentHandler(new ParseNonXML());
    pfp.parse(buildProperties());
  }

  public static Properties buildProperties() {
    Properties props = new Properties();
    for (int i = 0; i < 10; i++)
      props.setProperty("key" + i, "value" + i);
    return props;
  }

  public void startDocument() {
    System.out.println("<keys>");
  }

  public void endDocument() {
    System.out.println("</keys>");
  }

  public void characters(char[] data, int start, int end) {
    String str = new String(data, start, end);
    System.out.print(str);
  }

  public void startElement(String uri, String qName, String lName, Attributes atts) {
    System.out.print("<" + lName + ">");
  }

  public void endElement(String uri, String qName, String lName) {
    System.out.println("</" + lName + ">");
  }
}

class PropertyFileParser extends SAXParser {

  private Properties props = null;

  private ContentHandler handler = null;

  public void parse(Properties props) throws SAXException {
    handler = getContentHandler();
    handler.startDocument();
    Enumeration e = props.propertyNames();
    while (e.hasMoreElements()) {
      String key = (String) e.nextElement();
      String val = (String) props.getProperty(key);
      handler.startElement("", key, key, new AttributesImpl());
      char[] chars = getChars(val);
      handler.characters(chars, 0, chars.length);
      handler.endElement("", key, key);
    }
    handler.endDocument();
  }

  private char[] getChars(String value) {
    char[] chars = new char[value.length()];
    value.getChars(0, value.length(), chars, 0);
    return chars;
  }

}


           
       

Thank with us