Android:使用ISO-8859-1编码的SaxParser问题

时间:2010-05-25 10:21:14

标签: xml android parsing sax

我在使用android进行xml解析时面临一些问题。问题是来自服务器的xml带有“ISO-8859-1”设置为setEncoding(我得到<?xml version="1.0" encoding="ISO-8859-1"?>)格式,并且android设备似乎忽略了该编码。

例如,这是来自服务器的原始xml的一部分:

<Result Filename="Pautas para la Presentación RUP Iteraciones de Construcción.ppt">
        <Path>C:\Documents and Settings\zashael\My Documents\PFC\RUP\Pautas para la Presentación RUP Iteraciones de Construcción.ppt</Path>
        <Hostname>computer_1</Hostname>
        <IP>192.168.0.5:27960</IP>
        <ModDate>01-ene-1601 2:06:34</ModDate>
        <Size>33.280 bytes</Size>
    </Result>

这是我在解析xml之前得到的电话:

</Result>
 <Result Filename="Pautas para la Presentaci�n RUP Fase Inicio.ppt">
     <Path>C:\Documents and Settings\zashael\My Documents\PFC\RUP\Pautas para la Presentaci�n RUP Fase Inicio.ppt</Path>
     <Hostname>computer_1</Hostname>
     <IP>192.168.0.5:27960</IP>
     <ModDate>01-ene-1601 1:32:06</ModDate>
    <Size>26.624 bytes</Size>
 </Result>

正如您所看到的,“presentación”这个词有问题。

这是我接收文件的代码部分,然后将其发送给解析器:

do
                    {
                        auxMessage = ois.readObject();

                        if (auxMessage instanceof ComConstants)
                        {
                            receivedMessage = (ComConstants) auxMessage;

                            Log.d("Client", "Client has Search Results"); 

                         //Charset charset = Charset.forName("ISO-8859-1"); 
                         //CharsetDecoder decoder = charset.newDecoder(); 
                         //CharsetEncoder encoder = charset.newEncoder(); 



                            String test;

                            test = new String(
                                    receivedMessage.fileContent, 0,
                                    receivedMessage.okBytes);

                            if (finalMessage == null) {
                                finalMessage = test;
                            }
                            else {                          
                                finalMessage += test;
                            }


                         /*try { // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer 
                             // The new ByteBuffer is ready to be read. 
                             ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(finalMessage)); 
                             // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string. 
                             // The new ByteBuffer is ready to be read. 
                             CharBuffer cbuf = decoder.decode(bbuf); 
                             String s = cbuf.toString(); 

                            finalMessage = s;

                             } 

                         catch (CharacterCodingException e) { } 
                         }*/

                        }
                         else
                        {
                            Log.d("Client", "Unexpected message "
                                    + auxMessage.getClass().getName()); 

                            break;
                        }
                    } while (!receivedMessage.lastMessage);


                    //test encoding
                    //String s = finalMessage;
                    //finalMessage = new  String(s.getBytes("ISO-8859-1"));


                    System.out.println("antes de parsear" + finalMessage);

                    SaxParser sap = new SaxParser(finalMessage);

这是我的解析器代码:

package citic.android.remoteir;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

    public class SaxParser extends DefaultHandler{

        @SuppressWarnings("unchecked")
        ArrayList myResults;

        private String tempVal;

        private SearchResult tempResults;



        @SuppressWarnings("unchecked")
        public SaxParser(String xmlString){
            myResults = new ArrayList();

            parseDocument(xmlString);

            /* In order to test */
             printData();
        }

        @SuppressWarnings("unchecked")
        public ArrayList getResults(){

            return myResults;
        }



        private void parseDocument(String xmlString) {


            try {

                SAXParserFactory spf = SAXParserFactory.newInstance();

                spf.setFeature("http://xml.org/sax/features/namespaces",false);
                spf.setFeature("http://xml.org/sax/features/namespace-prefixes",true); 

                SAXParser sp = spf.newSAXParser();

                XMLReader xmlReader = sp.getXMLReader();
                xmlReader.setContentHandler(this);


                StringReader sr = new StringReader(xmlString);
                InputSource is = new InputSource(sr);
                is.setEncoding("ISO-8859-1");
                xmlReader.parse(is);


            }catch(SAXException se) {
                se.printStackTrace();
            }catch(ParserConfigurationException pce) {
                pce.printStackTrace();
            }catch (IOException ie) {
                ie.printStackTrace();
            }
        }


        @SuppressWarnings("unchecked")
        private void printData(){

        System.out.println("No of Results '" + myResults.size() + "'.");

        Iterator it = myResults.iterator();
        while(it.hasNext()) {
            System.out.println(((SearchResult) it.next()).toString());
            //System.out.println(it.next().toString());
        }
    }

        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            tempVal = "";
            if(qName.equalsIgnoreCase("Result")) {
                tempResults = new SearchResult();
                tempResults.setName(attributes.getValue("Filename"));
            }
        }


        public void characters(char[] ch, int start, int length) throws SAXException {

            tempVal = new String(ch,start,length);

        }

        @SuppressWarnings("unchecked")
        public void endElement(String uri, String localName, String qName) throws SAXException {

            if(qName.equalsIgnoreCase("Result")) {
                myResults.add(tempResults);


            }else if (qName.equalsIgnoreCase("Hostname")) {
                tempResults.setHostname(tempVal);
            }else if (qName.equalsIgnoreCase("IP")) {
                tempResults.setIpad(tempVal);
            }else if (qName.equalsIgnoreCase("Path")) {
                tempResults.setPath(tempVal);
            /*}else if (qName.equalsIgnoreCase("Author")) {
                tempResults.setHostname(tempVal);
            }else if (qName.equalsIgnoreCase("File")) {
                tempResults.setIpad(tempVal);
            */}else if (qName.equalsIgnoreCase("ModDate")) {
                tempResults.setModDate(tempVal);
            }else if (qName.equalsIgnoreCase("Size")) {
                tempResults.setSize((tempVal)); 
                }       
        }


}

我不知道该怎么做。我在收到xml字节到ISO编码后尝试设置我创建的字符串,但我唯一得到的是“square”而不是“ón”

比你!

1 个答案:

答案 0 :(得分:1)

服务器可以它是ISO-8859-1,但它看起来像是在发送UTF-8。

如果您可以控制服务器代码,请确保正确设置输出流上的编码。只需添加<?xml version="1.0" encoding="ISO-8859-1"?>标头,就不会导致输出的编码正确。