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
list.add(current);
List
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
List
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
// 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
list.add(current);
List
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
public static List
List
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);
// }
// }
}