Reputation: 15
In the web application service method, I am trying to parse a xml using VTD-XML. After reading it from the database, I'm calling the code below.
It's failing to parse and throws an exception:
UTF 8 encoding error: should never happen
The same code is working in separate class in main method. I am using NetBeans IDE.
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ebuild_woym_purple_clone1", "root", "root");
Statement st = con.createStatement();
st.executeQuery("select * from revision where name='First');
ResultSet rs = st.getResultSet();
if(rs.next()){
String xml = rs.getString(9);
System.out.println(xml);
vg.setDoc(xml.getBytes());
vg.parse(false); //vtd-xml parser object , throwing exception here
System.out.println("good");
}
Upvotes: 2
Views: 1769
Reputation: 159096
The error message indicates that the XML byte array is parsed using UTF-8 encoding, which is to be expected since that is the recommended encoding for XML.
However, when you call getBytes()
on a String
, you get (quoting javadoc):
Encodes this
String
into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
Since it is extremely unlikely that you default charset is UTF-8, you'll get something else, and parsing that as UTF-8 will then fail.
To fix it, use getBytes(Charset charset)
, as in:
vg.setDoc(xml.getBytes(StandardCharsets.UTF_8));
Upvotes: 3