Senin, 29 Desember 2008

XML Processing

since join on research engineer on detikcom, i modify JDOM API to support preprocessing search engine. I write code to read and write XML.
this is the code...

package jst.common.xml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class WriteXML {
Element elm;
Writer out = null;
static WriteXML w = new WriteXML();
static Document doc;
public Document setUpDocument(){
return new Document();
}
//set up new element
public Element setUpElement(String name){
elm = new Element(name);
return elm;
}

public void setUpChildElement(Element rootElement,Element elementName,String elementValue){
elementName.setText(elementValue);
rootElement.addContent(elementName);
}

public void addAtributElement(Element element,String atributName,String atributValue){
element.setAttribute(atributName, atributValue);
}

public void createELementXML(Element rootElement){
doc.setRootElement(rootElement);
}

public void writeXML(String fileaddress) throws IOException{
File fe = new File(fileaddress);
out = new BufferedWriter(new FileWriter(fe));
//writing xml file
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(doc,out);
out.close();
}
// public static void main(String[] args) {
// try {
//
// Element root,name,alamat,hobi;
// //creating document
// doc = w.setUpDocument();
// //writing element
// root = w.setUpElement("biodata");
// name = w.setUpElement("name");
// alamat = w.setUpElement("alamat");
// hobi = w.setUpElement("hobi");
// //building xml structure
// w.setUpChildElement(root,name,null);
// w.setUpChildElement(name, alamat, "jkt");
// w.setUpChildElement(root, hobi, "maen");
// w.addAtributElement(alamat, "lokasi", "lebak bulus");
// w.addAtributElement(hobi, "keterangan", "futsal");
// //create structure xml
// w.createELementXML(root);
// w.writeXML("/home/user/Documents/tes.xml");
// }
// catch (IOException e) {
// System.err.println(e);
// }
// }
}

package jst.common.xml;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/* Author : justin.aulia@gmail.com
*
* pada kelas ini terdapat dua method
* 1. getListChildElement --> rekursif element xml
* 2. getElementXML --> digunakan untuk mendapatkan element xml yang di simpan di List
* --> @return List
*
* cara penggunaan kelas ini
* - untuk mendapatkan element xmldeveloper hanya instance kelas ReadXML
* kemudian panggil aja method getElementXML(string_fileaddress_xml_yg_mo_dibaca)
* contoh di paling bawah
* */
public class ReadXML {
private static Element e;
//method ini digunakan untuk merekursif element xml
//method ini gak perlu di panggil!!krn udah digunakan
//di method getElementXML
private static List getListChildElement(Element current, int depth, List list) {
list.add(current);
List children = current.getChildren();
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Element child = (Element) iterator.next();
getListChildElement(child, depth+1,list);
}
return list;
}
//method ini mengembalikan list element XML file
//@return list
//parameter String fileaddress xml
public static List getElementXML(String fileAddress) throws JDOMException, IOException{
List list = new ArrayList();
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(fileAddress);
Element root = doc.getRootElement();
list = getListChildElement(root, 0,list);

return list;
}


// public static void main(String[] args) {
//
// Element e;
// String elmname,elmvalue;
// List listElemen = new ArrayList();
// ReadXML r = new ReadXML();
//
// try {
// listElemen = r.getElementXML("/home/user/Documents/masterkanal.xml");
//
// // printing all element
// Iterator iterator = listElemen.iterator();
// while (iterator.hasNext()) {
// e = (Element)iterator.next();
// //System.out.println(e);
//
// //mendapatkan nama elemen
// elmname = e.getName();
// //mendapatkan value elemen
// elmvalue = e.getText();
// //print out
// System.out.print(elmname +" : " + elmvalue);
//
// //get value atribut "url" di elemen "kanal"
// if(elmname=="kanal"){
// String value = e.getAttributeValue("url");
// System.out.println(" "+value);
// }
//
// }
// }
// // indicates a well-formedness error
// catch (JDOMException ex) {
// System.out.println(args[0] + " is not well-formed.");
// System.out.println(ex.getMessage());
// } catch (IOException ex) {
// System.out.println(ex);
// }
// }
}

package jst.common.xml;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class ElementLister {

public static void listChildren(Element current, int depth,HashMap hashKanal) {

String idkanal, url, idnews, title = null;
List children = current.getChildren();
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Element child = (Element) iterator.next();
idkanal = child.getAttributeValue("id");
url = child.getAttributeValue("url");
// if(child.getName()=="content"){
// idnews = child.getAttributeValue("id");
// if(child.getName()=="title"){
// title = child.getText();
// }
// hashTitle.put(idnews, title);
// }
hashKanal.put(idkanal, url);
listChildren(child, depth + 1,hashKanal);
}
}

private static List getListChildElement(Element current, int depth, List list) {

list.add(current);
List children = current.getChildren();
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Element child = (Element) iterator.next();
getListChildElement(child, depth+1,list);
//System.out.println(child);
}
return list;
}
//method ini mengembalikan list XML file
public static List getElementXML(String fileAddress) throws Exception{
List list = new ArrayList();
SAXBuilder builder = new SAXBuilder();

Document doc = builder.build(fileAddress);
Element root = doc.getRootElement();
list = getListChildElement(root, 0,list);

return list;
}

// public static void main(String[] args) {
//
// SAXBuilder builder = new SAXBuilder();
//
// List a = new ArrayList();
// //HashMap a = new HashMap();
//
// ElementLister el = new ElementLister();
// try {
//
// a = el.getElementXML("/home/user/Documents/masterkanal.xml");
//
// // printing all element
// Iterator iterator = a.iterator();
// while (iterator.hasNext()) {
// Element e = (Element)iterator.next();
// System.out.println(e);
//// if(e.getName()=="title"){
//// String key = e.getText();
//// System.out.println(key);
//// }
//// if(e.getName()=="content"){
//// String value = e.getAttributeValue("id");
//// System.out.println(value);
//// }
// }
// }
// // indicates a well-formedness error
// catch (JDOMException e) {
// System.out.println(args[0] + " is not well-formed.");
// System.out.println(e.getMessage());
// } catch (IOException e) {
// System.out.println(e);
// }
// }
}

Selasa, 09 September 2008

access dbms using jdbc

java have jdbc API to connect apps with rdbms. if we wanna use jdbc to connect dbms, there are many step that we must do it.
1. load the driver (jdbc driver must be specific with which rdbms we use)
2. open connection
3. create statement
4. execute statement
5. getting the result
6. close connection

example retrieve data
i use MySQL in this tutorial. i have database "tryout" and i was create table "mahasiswa"



package kpts;
import java.sql.*;

public class TesJDBC {

// Deklarasi atribut
Connection con = null;
ResultSet
rs = null;
Statement
stmt = null;
String
strSQL = "select * from mahasiswa order by nim";

// Konstruktor
public TesJDBC(){}

public void makeConnection() throws Exception {
Class.forName(
"com.mysql.jdbc.Driver");
con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/modul","root","");
}

public boolean getMahasiswa() throws Exception {
stmt = con.createStatement();
rs = stmt.executeQuery(strSQL);
return (rs != null);
}

public boolean getNextMahasiswa() throws Exception {
return rs.next(); //mengambil data pada row berikutnya
}

public String getColumn(String inCol) throws Exception {
return rs.getString(inCol); //mengambil data berdasarkan kolom
}
public void takeDown() throws Exception {
stmt.close(); //menutup statement
con.close(); //menutup koneksi
}
}

public static void main(String[] args) {

TesJDBC a = new TesJDBC();
try {
a.makeConnection();
if (a.getMahasiswa()) {
while (a.getNextMahasiswa()){
String nim = a.getColumn(
"nim");
String nama = a.getColumn(
"nama");
String alamat = a.getColumn(
"kelas");
String kelas = a.getColumn(
"kelas");
System.
out.print(nim+" "+nama+" "+alamat+" "+kelas);
System.
out.println();
}
}a.takeDown();

} catch (Exception e) {
e.printStackTrace();
}
}
}

live blogging in touring lab mabim

now i being presentation about my lab to mabimer's ittelkom
i try to explain about OpenSource especially java technology
cause core my lab was developing apps using java tech

i hope there are many mabimer's will appreciate about my presentation
so my lab will have a lot of members and we can build some products

justin start blogging

i try to force my self learn to share my acknowledgments

-jst-