BlackBerry разбирает XML-файл UTF-8 с помощью SAX Parser - PullRequest
0 голосов
/ 01 декабря 2011

Я пытаюсь проанализировать XML-файл UTF-8 с помощью SAX-парсера, и я использовал парсер, но он выдает исключение, это сообщение "Ожидается элемент"

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<config>
<filepath>/mnt/sdcard/Audio_Recorder/anonymous22242.3gp</filepath>
<filename>anonymous22242.3gp</filename>
<annotation>
    <file>anonymous22242.3gp</file>
    <timestamp>0:06</timestamp>
    <note>test1</note>
</annotation>
<annotation>
    <file>anonymous22242.3gp</file>
    <timestamp>0:09</timestamp>
    <note>لول</note>
</annotation>
<annotation>
    <file>anonymous22242.3gp</file>
    <timestamp>0:09</timestamp>
    <note>لولو</note>
</annotation>
</config> 


     private static String fileDirectory;
private final static ArrayList<String> allFileNames = new ArrayList<String>();
private final static ArrayList<String[]> allAnnotations = new ArrayList<String[]>();
private static String[] currentAnnotation = new String[3];

public static void main(String[] args) {
// TODO Auto-generated method stub
try {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser playbackParser = factory.newSAXParser();

    DefaultHandler handler = new DefaultHandler() {

        boolean audioFullPath = false;
        boolean audioName = false;
        boolean annotationFile = false;
        boolean annotationTimestamp = false;
        boolean annotationNote = false;

        public void startElement(String uri, String localName,
                String qName, Attributes attributes)
                throws SAXException {

            System.out.println("Start Element :" + qName);

            if (qName.equalsIgnoreCase("filepath")) {
                audioFullPath = true;
            }

            if (qName.equalsIgnoreCase("filename")) {
                audioName = true;
            }

            if (qName.equalsIgnoreCase("file")) {
                annotationFile = true;
            }

            if (qName.equalsIgnoreCase("timestamp")) {
                annotationTimestamp = true;
            }

            if (qName.equalsIgnoreCase("note")) {
                annotationNote = true;
            }

        }

        public void endElement(String uri, String localName,
                String qName) throws SAXException {

            System.out.println("End Element :" + qName);

        }

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

            if (audioFullPath) {
                String filePath = new String(ch, start, length);
                System.out.println("Full Path : " + filePath);
                fileDirectory = filePath;
                audioFullPath = false;
            }

            if (audioName) {
                String fileName = new String(ch, start, length);
                System.out.println("File Name : " + fileName);
                allFileNames.add(fileName);
                audioName = false;
            }

            if (annotationFile) {
                String fileName = new String(ch, start, length);
                currentAnnotation[0] = fileName;
                annotationFile = false;
            }

            if (annotationTimestamp) {
                String timestamp = new String(ch, start, length);
                currentAnnotation[1] = timestamp;
                annotationTimestamp = false;
            }
            if (annotationNote) {
                String note = new String(ch, start, length);
                currentAnnotation[2] = note;
                annotationNote = false;
                allAnnotations.add(currentAnnotation);
            }

        }

    };

    InputStream inputStream = getStream("http://www.example.com/example.xml");
    Reader xmlReader = new InputStreamReader(inputStream, "UTF-8");

    InputSource xmlSource = new InputSource(xmlReader);
    xmlSource.setEncoding("UTF-8");

    playbackParser.parse(xmlSource, handler);

    System.out.println(fileDirectory);
    System.out.println(allFileNames);
    System.out.println(allAnnotations);

} catch (Exception e) {
    e.printStackTrace();
}
}
}

public Static InputStream getStream(String url)
{
    try
    {
        connection = getConnection(url);
        connection.setRequestProperty("User-Agent",System.getProperty("microedition.profiles"));
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");

        inputStream = connection.openInputStream();
        return inputStream;
    }
    catch(Exception e)
    {
        System.out.println("NNNNNNN "+e.getMessage());

        return null;
      }

    }

 public HttpConnection getConnection(String url)
{

    try
    {
      connection = (HttpConnection) Connector.open(url+getConnectionString());


    }
    catch(Exception e)

    {

    }


    return connection;
}

но когда я передаю методу разбора inputStream вместо inputSource, он анализирует файл, но все еще имеет проблему с арабскими символами между

playbackParser.parse(inputStream, handler);

1 Ответ

1 голос
/ 01 декабря 2011

В показанном вами XML-коде есть арабские символы без кодировки. Это является нарушением объявленной кодировки XML, что означает, что XML искажен. Анализатор SAX последовательно обрабатывает данные по частям, вызывая события для каждой части. Он не обнаружит такую ​​ошибку кодирования, пока не достигнет фрагмента, содержащего эти ошибочные символы. С этим ничего не поделаешь. XML должен быть исправлен его первоначальным автором.

...