G
Ghislain Benrais
Hello,
I am new to java and I run a short program processing xml files.
Everything ran very well until I received xml files with the character
itself instead of its numerical reference (for instance 'é' instead of
'é'). I thought java would handle it but unexpectedly, it handles it
under DOS but doesn't handle it under Linux !
Do you have any explanations ?
Input file :
=======
<?xml version="1.0" encoding="ISO-8859-1" ?>
<values>
<value>détail</value>
<value>détail</value>
</values>
Java program :
==========
package javaapplication2;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;
public class Main extends DefaultHandler {
private String CData;
// Encodage
static String encoding;
private Writer out;
public Main(String[] args) {
super();
encoding = "ISO-8859-15";
try {
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler( this );
out = new OutputStreamWriter(new
FileOutputStream("out.txt"),encoding);
InputSource input = null;
input = new InputSource(new FileReader("file.xml"));
xr.parse(input);
out.close();
}catch ( Exception e ) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO code application logic here
Main main = new Main(args);
}
//--------------------------------------------------------------------------------------
// Méthodes du parser
//--------------------------------------------------------------------------------------
public void startElement( String namespaceURI, String localName, String
qName, Attributes attr ) throws SAXException {
CData = new String("");
}
public void characters(char[] chars, int iStart, int iLen) {
CData = CData + new String(chars, iStart, iLen);
}
public void endElement( String namespaceURI,String localName,String
qName ) throws SAXException {
if (localName.equals( "value" )) {
try{
out.write(CData+"\n");
}catch ( Exception e ) {
e.printStackTrace();
}
return;
}
}
}
Result if run from DOS
================
détail
détail
Result if run from Linux
=================
d?tail
détail
Thanks in advance,
Ghislain
I am new to java and I run a short program processing xml files.
Everything ran very well until I received xml files with the character
itself instead of its numerical reference (for instance 'é' instead of
'é'). I thought java would handle it but unexpectedly, it handles it
under DOS but doesn't handle it under Linux !
Do you have any explanations ?
Input file :
=======
<?xml version="1.0" encoding="ISO-8859-1" ?>
<values>
<value>détail</value>
<value>détail</value>
</values>
Java program :
==========
package javaapplication2;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;
public class Main extends DefaultHandler {
private String CData;
// Encodage
static String encoding;
private Writer out;
public Main(String[] args) {
super();
encoding = "ISO-8859-15";
try {
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler( this );
out = new OutputStreamWriter(new
FileOutputStream("out.txt"),encoding);
InputSource input = null;
input = new InputSource(new FileReader("file.xml"));
xr.parse(input);
out.close();
}catch ( Exception e ) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO code application logic here
Main main = new Main(args);
}
//--------------------------------------------------------------------------------------
// Méthodes du parser
//--------------------------------------------------------------------------------------
public void startElement( String namespaceURI, String localName, String
qName, Attributes attr ) throws SAXException {
CData = new String("");
}
public void characters(char[] chars, int iStart, int iLen) {
CData = CData + new String(chars, iStart, iLen);
}
public void endElement( String namespaceURI,String localName,String
qName ) throws SAXException {
if (localName.equals( "value" )) {
try{
out.write(CData+"\n");
}catch ( Exception e ) {
e.printStackTrace();
}
return;
}
}
}
Result if run from DOS
================
détail
détail
Result if run from Linux
=================
d?tail
détail
Thanks in advance,
Ghislain