SAXException无法获取文档编码

时间:2013-11-01 17:06:46

标签: java blackberry inputstream document sax

我正在尝试创建一个显示来自网站的新闻源的应用程序,因此我获取输入流并使用SAX在文档中解析它,但它返回SAX异常,它无法确定此Stream的编码类型。我之前尝试过将网站的流手动放入XML文件中并读取文件并且工作正常但是直接从Internet流式传输它会引发异常并且这是我的代码:

public final class MyScreen extends MainScreen {
protected static RichTextField RTF = new RichTextField("Plz Wait . . . ",
        Field.FIELD_BOTTOM);
public MyScreen() {
    // Set the displayed title of the screen
    super(Manager.NO_VERTICAL_SCROLL);

    setTitle("Yalla Kora");
    Runnable R = new Runnable();
    R.start();
    add(RTF);

}

private class Runnable extends Thread {

    public Runnable() {
        // TODO Auto-generated constructor stub
        ConnectionFactory factory = new ConnectionFactory();
        ConnectionDescriptor descriptor = factory
                .getConnection("http://www.yallakora.com/arabic/rss.aspx?id=0");

        HttpConnection httpConnection;
        httpConnection = (HttpConnection) descriptor.getConnection();// Connector.open("http://www.yallakora.com/pictures/main//2011/11/El-Masry-807-11-2011-21-56-7.jpg");
        Manager mainManager = getMainManager();
        RichList RL = new RichList(mainManager, true, 2, 1);

        InputStream input;

        try {
            input = httpConnection.openInputStream();

            Document document;

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder;
            try {
                docBuilder = docBuilderFactory.newDocumentBuilder();

                docBuilder.isValidating();
                try {
                    document = docBuilder.parse(input);

                    document.getDocumentElement().normalize();

                    NodeList item = document.getElementsByTagName("item");
                    int k = item.getLength();

                    for (int i = 0; i < k; i++) {

                        Node value = item.item(i);
                        NodeList Data = value.getChildNodes();

                        Node title = Data.item(0);
                        Node link = Data.item(1);
                        Node date = Data.item(2);
                        Node discription = Data.item(5);

                        Node Discription = discription.getFirstChild();

                        String s = Discription.getNodeValue();
                        int mm = s.indexOf("'><BR>");
                        int max = s.length();

                        String imagelink = s.substring(0, mm); 
                        String Khabar = s.substring(mm + 6, max);
                        String Date = date.getFirstChild().getNodeValue();
                        String Title = title.getFirstChild().getNodeValue();
                        String Link = link.getFirstChild().getNodeValue();

                        ConnectionFactory factory1 = new ConnectionFactory();
                        ConnectionDescriptor descriptor1 = factory1
                                .getConnection(imagelink);

                        HttpConnection httpConnection1;
                        httpConnection1 = (HttpConnection) descriptor1
                                .getConnection();
                        InputStream input1;

                        input1 = httpConnection1.openInputStream();
                        byte[] bytes = IOUtilities.streamToBytes(input1);
                        Bitmap bitmap = Bitmap.createBitmapFromBytes(bytes,
                                0, -1, 1);

                        ;
                        RL.add(new Object[] { bitmap, Title, Khabar, Date });
                        add(new RichTextField(link.getNodeValue(),
                                Field.NON_FOCUSABLE));

                    }

                    RTF.setText("");
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    RTF.setText("SAXException " + e.toString());
                    e.printStackTrace();
                }
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                RTF.setText("ParserConfigurationException " + e.toString());
                e.printStackTrace();
            }
        } catch (IOException e) {
            RTF.setText("IOException " + e.toString());
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}}

任何想法?

1 个答案:

答案 0 :(得分:0)

我建议将此代码重组为至少两部分。

我会创建一个下载函数,该函数被赋予一个URL并下载与该URL相关联的字节。这应该打开和关闭连接,只返回下载的字节或错误指示。

我会将此下载处理用作'函数调用'来下载XML字节。然后将获取的字节直接解析到解析器中。如果数据是正确构造的XML,它将有一个标题指示所使用的编码,因此您不必担心,解析器将应对。

解析后,再次使用下载功能下载与您想要的任何图像相关的字节。

关于SAX处理,您是否回顾过以下问题:

parse-xml-inputstream-in-blackberry-java-application