SAX decoding problem

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("\\<", "&lt;");
s = s.replaceAll(">", "&gt;");
s = s.replaceAll("'", "&apos;");
s = s.replaceAll("\"", "&quot;");
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);
}
});
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top