DOM နှင့် SAX နှစ်ခုစလုံးမှာ
သူဟာနှင့်သူ အားသာချက်၊ အားနည်းချက်တွေရှိပါတယ်။
DOM က document
structure တစ်ခုလုံးကို memory ပေါ်တင်တဲ့အတွက်ကြောင့် document structure ကြီ:ရင်
ကြီးသလောက် memory resource တွေလိုအပ်ပါတယ်။ ဒါပေမဲ့ သူက element တွေ တစ်ခုနှင့်တစ်ခုကြားမှာ
အလွယ်တကူသွားလာနိုင်ခြင်း၊ element content တွေကို modify (read/write) လုပ်ရတာလွယ်ကူခြင်းတို့ကြောင့်
အသုံးများပါတယ်။ (ဥပမာ - application တစ်ခုရဲ့ system configure file)
SAX ကတော့ document
structure တစ်ခုလုံးကို memory ပေါ်မတင်တဲ့အတွက် less-memory intensive ဘဲလိုအပ်ပါတယ်။
SAX ကိုများသောအားဖြင့် element content များတဲ့ XML document တွေကို parse လုပ်တဲ့နေရာမှာအသုံးပြုပါတယ်။
SAX parser တွေက XML document ကို modify (Read only) လုပ်မပေးနိုင်ပါဘူး။ (ဥပမာ directory
data file )
|
DOM
|
SAX
|
Read Mode
|
Reads
entire Document
|
Reads Node
by Node
|
XML File
size
|
Small/Medium
XML File
|
Large XML
File
|
Parser
Type
|
Tree based
Parser
|
Event
based Parser
|
Speed
|
Little
Slow
|
Faster
|
Read
|
Yes
|
Yes
|
Write
|
Yes
|
No
|
DOM Parser Java Example Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
| package XML;
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class DOMparser {
public static void main(String [] args){
try{
//Read File
File xmlFile=new File("catalog.xml");
//DocumentBuilderFactory
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//DocumentBuilder
DocumentBuilder builder=factory.newDocumentBuilder();
//Document
Document document=builder.parse(xmlFile);
//Get Root Element
Element rootElement=document.getDocumentElement();
System.out.println("Root Elemnet :" + rootElement.getTagName());
parseNode(null,rootElement);
}catch(Exception e){
e.printStackTrace();
}
}
public static void parseNode(Element previousNode,Element visitNode){
if(previousNode != null){
System.out.println("Element "+ previousNode.getTagName() + " has element :");
}
System.out.println("Element Name :" + visitNode.getTagName());
//List Attributes From VisitNode
if(visitNode.hasAttributes()){
System.out.println("-Elment " + visitNode.getTagName() +" has Attributes :");
NamedNodeMap attributes=visitNode.getAttributes();
//Extract Attributes
for(int i=0; i<attributes.getLength(); i++){
Attr attribute=(Attr)(attributes.item(i));
//Print Attribute
System.out.println("--Attribute :" + attribute.getName() + " = " + attribute.getValue());
}
}//End of Attributes List
//Read Nested Element or Child Node
//Get Child Node List
NodeList nodeList=visitNode.getChildNodes();
for(int i=0; i<nodeList.getLength(); i++){
//Get Child Node
Node node=nodeList.item(i);
if(node.getNodeType()==Node.ELEMENT_NODE){
Element element=(Element)node;
//Do same Thing /Revcursive Algorithm
parseNode(visitNode,element);
}
else if(node.getNodeType()==Node.TEXT_NODE){
String str=node.getNodeValue().trim();
if(str.length()>0){
System.out.println("---Element Text: " + str);
}
}
}
}
}
|
SAX Parser Java Example Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
| package XML;
import org.xml.sax.*;
import javax.xml.parsers.*;
import org.xml.sax.helpers.*;
import java.io.*;
public class SAXParser {
public static void main(String []args){
SAXParser saxParser=new SAXParser();
saxParser.parseDocument();
}
public void parseDocument(){
try{
//SAX Parser Factory
SAXParserFactory factory=SAXParserFactory.newInstance();
//SAX Parser
SAXParser parser=factory.newSAXParser();
//Handler **
DefaultHandler handler=new CustomSAXHandler();
//Set File to Parse and Set Handler
parser.parse(new File("catalog.xml"), handler);
}catch(Exception e){
e.printStackTrace();
}
}
private class CustomSAXHandler extends DefaultHandler {
//Constructor
public CustomSAXHandler(){
}
//Start Document
public void startDocument() throws SAXException {
System.out.println("Event Type : Start Document ");
}
//End Document
public void endDocument() throws SAXException {
System.out.println("Event Type : End Document ");
}
//Start Element
public void startElement (String uri,String localName,String qName,Attributes attributes){
System.out.println("Event Type: Start Element");
System.out.println("Element Name:" + qName);
for (int i = 0; i < attributes.getLength(); i++) {
System.out.println("Attribute Name:" + attributes.getQName(i));
System.out.println("Attribute Value:" + attributes.getValue(i));
}
}
//End Element
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("Event Type: End Element");
}
//Get Text
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println("Event Type: Text");
String str = (new String(ch, start, length));
System.out.println(str);
}
//Error
public void error(SAXParseException e)
throws SAXException{
System.out.println("Error "+e.getMessage());
}
public void fatalError(SAXParseException e)
throws SAXException{
System.out.println("Fatal Error "+e.getMessage());
}
public void warning(SAXParseException e)
throws SAXException{
System.out.println("Warning "+e.getMessage());
}
}
}
|
|