In this tutorial we are going to see how to read XML file with UTF-8 character using SAX parser.
1. Create Project Called JavaXML.
2. Create package called com.javatutorialscorner.xml.sax under JavaXML.
3. Create Java class called ReadUTF8XMLFile.java under com.javatutorialscorner.xml.sax package.
ReadUTF8XMLFile.java
01.
package
com.javatutorialscorner.xml.sax;
02.
03.
import
java.io.File;
04.
import
java.io.FileInputStream;
05.
import
java.io.IOException;
06.
import
java.io.InputStream;
07.
import
java.io.InputStreamReader;
08.
import
java.io.Reader;
09.
10.
import
javax.xml.parsers.ParserConfigurationException;
11.
import
javax.xml.parsers.SAXParser;
12.
import
javax.xml.parsers.SAXParserFactory;
13.
14.
import
org.xml.sax.Attributes;
15.
import
org.xml.sax.InputSource;
16.
import
org.xml.sax.SAXException;
17.
import
org.xml.sax.helpers.DefaultHandler;
18.
19.
public
class
ReadUTF8XMLFile {
20.
21.
/**
22.
* @param args
23.
*/
24.
public
static
void
main(String[] args) {
25.
// TODO Auto-generated method stub
26.
try
{
27.
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
28.
SAXParser parser = saxParserFactory.newSAXParser();
29.
DefaultHandler defaultHandler =
new
DefaultHandler() {
30.
boolean
firstName =
false
;
31.
boolean
lastName =
false
;
32.
boolean
petName =
false
;
33.
34.
public
void
startElement(String uri, String localName,
35.
String qName, Attributes attribute)
throws
SAXException {
36.
System.out.println(
"Start Element : "
+ qName);
37.
if
(qName.equalsIgnoreCase(
"FIRSTNAME"
)) {
38.
firstName =
true
;
39.
}
40.
if
(qName.equalsIgnoreCase(
"LASTNAME"
)) {
41.
lastName =
true
;
42.
}
43.
if
(qName.equalsIgnoreCase(
"PETNAME"
)) {
44.
petName =
true
;
45.
}
46.
}
47.
48.
public
void
endElement(String uri, String localName,
49.
String qName)
throws
SAXException {
50.
System.out.println(
"End Element : "
+ qName);
51.
}
52.
53.
public
void
characters(
char
ch[],
int
start,
int
length)
54.
throws
SAXException {
55.
if
(firstName) {
56.
System.out.println(
"First Name : "
57.
+
new
String(ch, start, length));
58.
firstName =
false
;
59.
}
60.
if
(lastName) {
61.
System.out.println(
"Last Name : "
62.
+
new
String(ch, start, length));
63.
lastName =
false
;
64.
}
65.
if
(petName) {
66.
System.out.println(
"Pet Name : "
67.
+
new
String(ch, start, length));
68.
petName =
false
;
69.
70.
}
71.
}
72.
};
73.
File xmlFile =
new
File(
"C:\\jtc\\student.xml"
);
74.
InputStream inputStream =
new
FileInputStream(xmlFile);
75.
Reader reader =
new
InputStreamReader(inputStream,
"UTF-8"
);
76.
InputSource inputSource =
new
InputSource(reader);
77.
parser.parse(inputSource, defaultHandler);
78.
}
catch
(ParserConfigurationException e) {
79.
// TODO Auto-generated catch block
80.
e.printStackTrace();
81.
}
catch
(SAXException e) {
82.
// TODO Auto-generated catch block
83.
e.printStackTrace();
84.
}
catch
(IOException e) {
85.
// TODO Auto-generated catch block
86.
e.printStackTrace();
87.
}
88.
89.
}
90.
91.
}
If you parse XML file that contains UTF character that throws error.So avoid this error convert xml to UTF-8 InputStream by using following lines of code.
1.
File xmlFile =
new
File(
"C:\\jtc\\student.xml"
);
2.
InputStream inputStream =
new
FileInputStream(xmlFile);
3.
Reader reader =
new
InputStreamReader(inputStream,
"UTF-8"
);
4.
InputSource inputSource =
new
InputSource(reader);
5.
parser.parse(inputSource, defaultHandler);
student.xml
1.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
standalone
=
"no"
?>
2.
<
college
>
3.
<
student
>
4.
<
firstname
>Shiva</
firstname
>
5.
<
lastname
>J</
lastname
>
6.
<
petname
>§</
petname
>
7.
</
student
>
8.
</
college
>
Now you can run the program see the following output in console.
Start Element : college
Start Element : student
Start Element : firstname
First Name : Shiva
End Element : firstname
Start Element : lastname
Last Name : J
End Element : lastname
Start Element : petname
Pet Name : §
End Element : petname
End Element : student
End Element : college
0 comments:
Post a Comment