F
Frank Cornelis
Hi,
I have this problem that I cannot decode special characters like
ë and ä using the SAX XML parser. (See the example)
When I try to decode those special characters I always get:
ë ä
How to solve this problem?
Regards,
Frank.
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
class Test {
final static String FILENAME = "test.xml";
private static String encode(String s) {
if (s == null)
return "";
s = s.replaceAll("&", "&");
s = s.replaceAll("\\<", "<");
s = s.replaceAll(">", ">");
s = s.replaceAll("'", "'");
s = s.replaceAll("\"", """);
return s;
}
public static void main(String [] args) throws ParserConfigurationException, SAXException, FileNotFoundException, IOException {
String xmlStr = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
xmlStr += "<body>" + encode("hello world ë ä: 1 < 2") + "</body>";
System.out.println(xmlStr);
new PrintStream(new FileOutputStream(FILENAME)).println(xmlStr);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(new File(FILENAME), new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) {
System.out.println("startElement: " + qName);
content = "";
}
public void endElement(String uri, String localName, String qName) {
System.out.println("content: " + content);
System.out.println("endElement: " + qName);
}
String content;
public void characters(char[] ch, int start, int length) {
content += new String(ch, start, length);
}
});
}
}
I have this problem that I cannot decode special characters like
ë and ä using the SAX XML parser. (See the example)
When I try to decode those special characters I always get:
ë ä
How to solve this problem?
Regards,
Frank.
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
class Test {
final static String FILENAME = "test.xml";
private static String encode(String s) {
if (s == null)
return "";
s = s.replaceAll("&", "&");
s = s.replaceAll("\\<", "<");
s = s.replaceAll(">", ">");
s = s.replaceAll("'", "'");
s = s.replaceAll("\"", """);
return s;
}
public static void main(String [] args) throws ParserConfigurationException, SAXException, FileNotFoundException, IOException {
String xmlStr = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
xmlStr += "<body>" + encode("hello world ë ä: 1 < 2") + "</body>";
System.out.println(xmlStr);
new PrintStream(new FileOutputStream(FILENAME)).println(xmlStr);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(new File(FILENAME), new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) {
System.out.println("startElement: " + qName);
content = "";
}
public void endElement(String uri, String localName, String qName) {
System.out.println("content: " + content);
System.out.println("endElement: " + qName);
}
String content;
public void characters(char[] ch, int start, int length) {
content += new String(ch, start, length);
}
});
}
}