Menampilkan data XML
Diketahui
file
product.xml berikut
<PRODUCTDETAILS>
<PRODUCT>
<PRODID> P001
</PRODID>
<PRODNAME>
Beras </PRODNAME>
<PRICE> 5000 </PRICE>
</PRODUCT>
<PRODUCT>
<PRODID> P002
</PRODID>
<PRODNAME>
Jagung </PRODNAME>
<PRICE> 4000 </PRICE>
</PRODUCT>
<PRODUCT>
<PRODID> P003
</PRODID>
<PRODNAME>
Kedelai </PRODNAME>
<PRICE> 4500 </PRICE>
</PRODUCT>
</PRODUCTDETAILS>
Ditampilkan
dengan
Mozilla, product.xml akan
nampak sebagai
berikut:
Bagaimana menampilkannya menggunakan
Java? Buat program TampilProduk.java berikut.
import java.io.*;
//package-package yang diperlukan
untuk
mengakses SAX
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class TampilProduk extends DefaultHandler
{
static private Writer writerOut;
StringBuffer stringBuffer;
/* Masukan nama file XML */
public static void main(String fileName[])
{
if (fileName.length != 1)
{
System.err.println("Usage: cmd filename");
System.exit(1);
}
DefaultHandler defaultHandler = new TampilProduk(); SAXParserFactory saxParserFactory =
SAXParserFactory.newInstance();
try
{
/* untuk tampilan
*/
writerOut =
new OutputStreamWriter(System.out,"UTF8");
/* buat objek SAXParser class */
SAXParser Sax_Parser =
saxParserFactory.newSAXParser();
/* telusuri file XML */
Sax_Parser.parse( new
File(fileName[0]), defaultHandler);
}
catch (Throwable thr)
{
thr.printStackTrace();
}
System.exit(0);
}
/* Method yang menangani startDocument */
public void startDocument()
throws SAXException
{
IOHandler("<?xml version = '1.0' encoding =
'UTF-8'?>");
newLine();
}
/* Method yang menangani endDocument */
public void endDocument()
throws SAXException
{
try
{
newLine();
writerOut.flush();
}
catch (IOException e)
{
throw new
SAXException("I/O error", e);
}
}
/* Method yang menangani startElement */
public void
startElement(String
namespaceURI, String sName,
String
qName, Attributes
attrs) throws SAXException
{
displayText();
String eName = sName;
if ("".equals(eName))
eName = qName; IOHandler("<" + eName);
if (attrs != null)
{
for (int i
= 0; i < attrs.getLength(); i++)
{
String aName = attrs.getLocalName(i);
if ("".equals(aName))
aName = attrs.getQName(i);
IOHandler("
");
IOHandler(aName + "=\"" + attrs.getValue(i)
+ "\"");
}
}
IOHandler(">");
}
/* Method yang menangani endElement */
public void
endElement(String
namespaceURI, String sName,
String
qName)
throws SAXException
{
displayText();
String eName = sName;
if ("".equals(eName))
eName = qName; IOHandler("</" + eName + ">");
}
/* Method yang menangani characters */
public void characters(char buf[],
int
offset, int len)
throws SAXException
{
String s
= new String(buf, offset,
len);
if (stringBuffer == null)
{
stringBuffer = new StringBuffer(s);
}
else
{
stringBuffer.append(s);
}
}
/* Tampilkan text yang terkumpul
di string buffer */
private void displayText() throws SAXException
{
if (stringBuffer == null)
return;
String s
= ""+stringBuffer;
IOHandler(s);
stringBuffer = null;
}
/* Tulis isi
string buffer ke layar */
private void IOHandler(String s) throws SAXException
{
try
{
writerOut.write(s);
writerOut.flush();
}
catch (IOException IOExcep)
{
throw new
SAXException("I/O error", IOExcep);
}
}
/* Buat baris baru
*/
private void newLine() throws SAXException
{
String lineEnd = System.getProperty("line.separator");
try
{
writerOut.write(lineEnd);
}
catch (IOException e)
{
throw new
SAXException("I/O error", e);
}
}
}
Kompilasi program di
atas dengan
:
C>javac TampilProduk.java
Jalankan
program di
atas dengan :
C>java TampilProduk product.xml
Hasilnya adalah sebagai berikut:
Tampilkan Data XML tanpa tag
Jika ingin
ditampilkan produk tanpa tag,
bagaimana caranya?
Kode berikut, TampilProduk2.java dapat digunakan
untuk
maksud tersebut.
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class TampilProduk2 extends DefaultHandler
{
StringBuffer stringBuffer;
public static void main(String fileName[])
{
if (fileName.length != 1)
{
System.err.println("Usage: cmd filename"); System.exit(1);
}
DefaultHandler defaultHandler = new TampilProduk2(); SAXParserFactory saxParserFactory =
SAXParserFactory.newInstance();
try
{
SAXParser Sax_Parser =
saxParserFactory.newSAXParser(); Sax_Parser.parse( new
File(fileName[0]), defaultHandler);
}
catch (Throwable thr)
{
thr.printStackTrace();
}
System.exit(0);
}
public void startDocument() throws SAXException
{
}
public void endDocument()
throws SAXException
{
System.out.println(stringBuffer.toString());
}
public
void startElement(String namespaceURI,
String sName, String
qName, Attributes
attrs) throws SAXException
{
}
public
void endElement(String namespaceURI,
String sName, String
qName)
throws SAXException
{
}
public void characters(char buf[],
int
offset, int len)
throws SAXException
{
String s
= new String(buf, offset, len);
if (stringBuffer == null)
{
stringBuffer = new StringBuffer(s);
}
else
{
stringBuffer.append(s);
}
}
}
Kompilasi dan jalankan
dengan :
C:\java TampilProduk2 product.xml
Hasilnya adalah sebagai berikut.
Perhitungan
terhadap data XML
Diketahui
data item.xml berikut:
<ItemsInfo>
<Item>
<type>Book</type>
<price>100</price>
</Item>
<Item>
<type>CD</type>
<price>600</price>
</Item>
<Item>
<type>Greeting Card</type>
<price>500</price>
</Item>
</ItemsInfo>
Bagaimana menjumlahkan
data-data price?
Kode HitungPrice.java
berikut dapat digunakan.
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
public class HitungPrice extends DefaultHandler
{
StringBuffer stringBuffer =
new StringBuffer();
private int amt=0;
public static void main(String fileName[])
{
if (fileName.length != 1)
{
System.err.println("Usage: cmd filename");
System.exit(1);
}
DefaultHandler defaultHandler = new HitungPrice();
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try
{
SAXParser Sax_Parser =
saxParserFactory.newSAXParser(); Sax_Parser.parse( new
File(fileName[0]), defaultHandler);
}
catch (Throwable thr)
{
thr.printStackTrace();
}
System.exit(0);
}
public void startDocument() throws SAXException
{
}
public void endDocument() throws SAXException
{
System.out.println("The total amount is:"+amt);
}
public
void startElement(String namespaceURI,
String
sName, String qName, Attributes attrs)
throws SAXException
{
stringBuffer.setLength(0);
}
public void
endElement(String
namespaceURI, String sName,
String
qName)
throws SAXException
{
if(qName.equals("price") && !qName.equals(""))
amt += Integer.parseInt(stringBuffer.toString().trim());
stringBuffer.setLength(0);
}
public void characters(char buf[],
int
offset, int len)
throws SAXException
{
stringBuffer.append(buf, offset,
len);
}
}
Jika dijalankan akan menghasilkan: