Monday, January 31, 2011

Create File XML

Extensible Markup Language (XML) is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications.
The material in this section is based on the XML Specification. This is not an exhaustive list of all the constructs which appear in XML; it provides an introduction to the key constructs most often encountered in day-to-day use. XML documents may begin by declaring some information about themselves, as in the following example.
<?xml version="1.0" encoding="UTF-8" ?>
view raw header_xml hosted with ❤ by GitHub
In Java, two built-in XML parsers are available – DOM and SAX, both have their pros and cons. Here’s few examples to show how to create, modify and read a XML file with Java DOM, SAX and JDOM xml parser.
The DOM interface is the easiest to understand. It parses an entire XML document and load it into memory, modeling it with Object for the traversal. DOM Parser is slow and consume a lot memory if it load a XML document which contains a lot of data.
now we try to create XML files using XML DOM Parser.

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class WriteXMLFileOK {
public static void main(String argv[]) {
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); //to define the value xml
DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); //to define the value xml
//root elements
Document doc = docBuilder.newDocument(); //to define the value xml
Element rootElement = doc.createElement("RootUtamaMain");
doc.appendChild(rootElement);
//Mahasiswa elements
Element mahasiswa = doc.createElement("Mahasiswa");
rootElement.appendChild(Mahasiswa);
//set attribute to Mahasiswa element
Attr attr = doc.createAttribute("nik");
attr.setValue("10001");
mahasiswa.setAttributeNode(attr);
//shorten way
//mahasiswa.setAttribute("id", "10001");
//firstname elements
Element firstname = doc.createElement("firstname");
firstname.appendChild(doc.createTextNode("Dini"));
mahasiswa.appendChild(firstname);
//lastname elements
Element lastname = doc.createElement("lastname");
lastname.appendChild(doc.createTextNode("Dyena"));
mahasiswa.appendChild(lastname);
//nickname elements
Element nickname = doc.createElement("nickname");
nickname.appendChild(doc.createTextNode("DinDin"));
mahasiswa.appendChild(nickname);
//write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
//StreamResult result = new StreamResult(System.out); //this if debug
StreamResult result = new StreamResult(new File("C:\\testing.xml")); //output file testing.xml
transformer.transform(source, result);
System.out.println("Done");
}catch(ParserConfigurationException pce){
pce.printStackTrace();
}catch(TransformerException tfe){
tfe.printStackTrace();
}
}
}
view raw xmlfile hosted with ❤ by GitHub


results :

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<RootUtamaMain>
<Mahasiswa nik="10001">
<firstname>Dini</firstname>
<lastname>Dyena</lastname>
<nickname>DinDin</nickname>
</Mahasiswa>
</RootUtamaMain>
view raw resultXML hosted with ❤ by GitHub

XML file can be edited for each element in its node can query the results table. XML file from the query table.
//Class clXml
public clXml() {
String no=null;
String kec =null;
try {
conn = new sambungDBLog(); //Connection Database
String query = "select * from dataKecamatan";
ResultSet rs = (ResultSet) conn.state.executeQuery(query);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //to define the value xml
DocumentBuilder db = dbf.newDocumentBuilder(); //to define the value xml
Document doc = db.newDocument(); //to define the value xml
Element rootElement = doc.createElement("Data"); // Data is root from element
doc.appendChild(rootElement);
while (rs.next()) { //result Query
no = rs.getString(1);
kec = rs.getString(2);
Element number = doc.createElement("No"); //No element
number.appendChild(doc.createTextNode(no)); // No from query table
rootElement.appendChild(number); //number include rootElement
Element kecamatan = doc.createElement("Kecamatan"); //kecamatan element
kecamatan.appendChild(doc.createTextNode(kec)); //kec from query table
rootElement.appendChild(kecamatan); //Kec include rootElement
view raw xmlQuery hosted with ❤ by GitHub

No comments:

Post a Comment