/
TestXML.java
executable file
·85 lines (66 loc) · 2.24 KB
/
TestXML.java
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
// ===================================
// PROJET QUESACO
// @autors : Laura BENDHAIBA
// Lionel LONKAP TSAMBA
// ===================================
package applications;
//Imports
//========================================================
import javax.swing.JFileChooser;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import parser.myXMLHandler;
import tools.Msg;
import org.xml.sax.SAXException;
public class TestXML {
// Attributes
// ========================================================
public static String uri;
static myXMLHandler handler;
// Methods
// ========================================================
// getParser
public static SAXParser getParser()
throws ParserConfigurationException, SAXException {
SAXParserFactory factory = SAXParserFactory.newInstance();
return (SAXParser) factory.newSAXParser();
}
/**
* parse: to parse the XML file
* @throws SAXException
*/
public static void parse() throws SAXException {
// open dialog to choose your XML file
_openDialogTochooseXMLFile();
// set the parser handler
handler = new myXMLHandler();
try {
Msg.puts(":: Start parsing file at << "+ uri +" >>");
TestXML.getParser().parse(uri, handler);
}
catch ( Exception e) {
Msg.puts(" Parsing error: " + e.getMessage() );
}
}
// Main
// ------------------------------------------------------
public static void main(String[] args) throws Exception {
Msg.puts(" =====================================================\n" +
" == MyXMLParser : XML file parser ==\n" +
" =====================================================\n");
Msg.putsLine();
TestXML.parse();
Msg.putsLine();
Msg.puts(":: File parsed successfully !!!");
}
public static void _openDialogTochooseXMLFile() {
JFileChooser chooser = new JFileChooser();
int retVal = chooser.showOpenDialog(chooser);
if(retVal == JFileChooser.APPROVE_OPTION) {
TestXML.uri = chooser.getSelectedFile().getAbsolutePath();
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
}
}