Witam,
Mam problem z polskimi znakami przy parsowaniu XML'a.
Właściwie powinno działać, bo pliki to modyfikacje z tutoriali dostępnych w sieci i działa z zastrzeżeniem do polskich znaków. Nie są one wyświetlane prawidłowo ani na konsole, ani do pola tekstowego.
Kod poniżej. Jakieś pomysły?
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
public class MySAXApp extends DefaultHandler implements ActionListener
{
protected
JTextArea textArea;
static JEditorPane jep = new JEditorPane();
public static void main (String args[])
throws Exception
{
XMLReader xr = XMLReaderFactory.createXMLReader();
MySAXApp handler = new MySAXApp();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
final JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.showDialog(fc, "Wybierz pliki do archwizacji");
File file = fc.getSelectedFile();
//JEditorPane jep = new JEditorPane();
FileReader r = new FileReader(file.getAbsolutePath());
xr.parse(new InputSource(r));
System.out.println(12);
}
public MySAXApp ()
{
super();
textArea = new JTextArea();
JScrollPane scrollingArea = new JScrollPane(textArea);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.add(textArea);
frame.add(scrollingArea);
frame.pack();
frame.setVisible(true);
frame.setSize(300, 200);
}
////////////////////////////////////////////////////////////////////
// Event handlers.
////////////////////////////////////////////////////////////////////
public void startDocument ()
{
System.out.println("Początek dokumentu");
textArea.append("Poczatek dokumentu\n");
}
public void endDocument ()
{
System.out.println("Koniec dokumentu");
textArea.append("Koniec dokumentu \n");
}
public void startElement (String uri, String name,
String qName, Attributes atts)
{
if (qName.equalsIgnoreCase("Grupa")){
System.out.println(name + " : " +atts.getValue("nazwa"));
textArea.append(name + ": " +atts.getValue("nazwa") + "\n");
} else if (name.equals("Cena")){
System.out.println("nazwa:");
textArea.append("\t");
}
}
public void endElement (String uri, String name, String qName)
{
if(qName.equalsIgnoreCase("Produkt")){
System.out.println("\t");
//textArea.append("\t");
} else if (qName.equalsIgnoreCase("Cena")){
//System.out.println("\n");
textArea.append("\n");
} else if (qName.equalsIgnoreCase("Grupa")){
textArea.append("\n");
}
/*if ("".equals (uri))
System.out.println("Koniec elementu: " + qName);
else
System.out.println("Koniec elementu: {" + uri + "}" + name);*/
}
public void characters (char ch[], int start, int length)
{
String toSay="";
//System.out.print("Wartość pola: \"");
for (int i = start; i < start + length; i++) {
switch (ch[i]) {
case '\\':
System.out.print("");
break;
case '\"':
System.out.print("1");
break;
case '\n':
System.out.print("");
break;
case '\r':
System.out.print("");
break;
case '\t':
System.out.print("");
break;
default:
if(ch[i]== 22){
System.out.println(123);
}else
System.out.print(ch[i]);
toSay = toSay + ch[i];
break;
}
}
if(toSay != null)
textArea.append(toSay);
System.out.print("");
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}