Results 1 to 2 of 2
  1. #1
    damiannelus is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default XML Parser SAX problem with diacritical letters

    Hi there,
    I used few tutorials from net to build XML Parser. Everything works fine, with small exception. Either system.out or JTextArea doesn't show polish diacritical letters - it's important for me to solve this problem. Anyone have idea, what should be changed?

    Code here:
    Java Code:
    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("Początek 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 = new String(ch, start, length);
    	//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 ="" + ch[i];
    	    	textArea.append(toSay);
    		break;
    	    }
    	}
    	if(toSay != null)
    		
    	//textArea.append(toSay);
    	System.out.print("");
        }
    
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    }

  2. #2
    sztejkat is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default

    1. Make sure, yours source code editor is set to store files in UTF-8 format.
    2. Make sure, javac -encoding UTF-8 is used to compile source file.


    This way You will have correct encoding in swing and GUI. However I/O streams to console may be incorrect in Windows operating system.

    3. Better way: read the i18n (internationalization-localization) guides and follow them.

Similar Threads

  1. xml parser problem
    By raghavendra.cse in forum Advanced Java
    Replies: 1
    Last Post: 05-13-2010, 05:01 PM
  2. Problem with HTML parser
    By kpraveenreddy in forum New To Java
    Replies: 0
    Last Post: 03-02-2010, 03:12 PM
  3. What does the letters mean?
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 02-11-2010, 10:50 PM
  4. need block letters??
    By dc2acgsr99 in forum New To Java
    Replies: 16
    Last Post: 01-29-2008, 09:31 AM
  5. problem with XERCES Parser using Option FieldType
    By oregon in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 02:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •