繁体   English   中英

意外令牌:java输入流阅读器中的位置文本

[英]Unexpected token: position text in java input stream reader

我正在尝试使用Pullparser从URL解析xml,但是出现以下错误:

意外的令牌(java.io.InputStreamReader中的position:TEXT-!! .....)

这是我用来处理xml文件的代码:

private class GetXMLTask extends AsyncTask<String, Void, String> {
        private Activity context;

        public GetXMLTask(Activity context) {
            this.context = context;
        }

        @Override
        protected String doInBackground(String... urls) {
            String xml = null;
            for (String url : urls) {
                xml = getXmlFromUrl(url);
            }
            return xml;
        }

        @Override
        protected void onPostExecute(String xml) {

            XMLDOMParser parser = new XMLDOMParser();
            InputStream stream = new ByteArrayInputStream(xml.getBytes());
            Document doc = parser.getDocument(stream);

            NodeList nodeList = doc.getElementsByTagName(NODE_CUST);

            Customers = new ArrayList<Customer>();
            Customer customer = null;
            for (int i = 0; i < nodeList.getLength(); i++) {
                customer = new Customer();
                Element e = (Element) nodeList.item(i);
                customer.setId(Integer.parseInt(e.getAttribute(ATTR_ID)));
                customer.setName(parser.getValue(e, NODE_NAME));
                customer.setSurname(parser.getValue(e, NODE_SUR));

                Customers.add(customer);
            }

            listViewAdapter = new CustomListViewAdapter(context, Customers);
            listView.setAdapter(listViewAdapter);
        }

        /* uses HttpURLConnection to make Http request from Android to download
         the XML file */
        private String getXmlFromUrl(String urlString) {....java
            StringBuffer output = new StringBuffer("");

            InputStream stream = null;
            URL url;
            try {
                url = new URL(urlString);
                URLConnection connection = url.openConnection();

                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                httpConnection.setRequestMethod("GET");
                httpConnection.connect();

                if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    stream = httpConnection.getInputStream();
                    BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));
                    String s = "";
                    while ((s = buffer.readLine()) != null)
                        output.append(s);
                }
            } catch (MalformedURLException e) {
                Log.e("Error", "Unable to parse URL", e);
            } catch (IOException e) {
                Log.e("Error", "IO Exception", e);
            }

            return output.toString();
 }
}

这行似乎是问题所在:

  Document doc = parser.getDocument(stream);
  The getDocumet(stream) method is throwing the exception:

   public Document getDocument(InputStream inputStream) {
    Document document = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder db = factory.newDocumentBuilder();
        InputSource inputSource = new InputSource(inputStream);

        document = db.parse(inputSource);
    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage(), e);
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage(), e);
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage(), e);
        return null;
    }
    return document;
}

xml的视图源看起来都不像是这样: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection><customer><id>4</id><name>john</name><surname>test</surname></customer><customer><id>6</id><name>test</name><surname>tset</surname></customer><customer><id>8</id><name>test</name><surname>test</surname></customer><customer><id>9</id><name>brian</name><surname>brian</surname></customer><customer><id>10</id><name>test</name><surname>testr</surname></customer><customer><id>11</id><name>hello</name><surname>pass</surname></customer><customer><id>12</id><name>brian2</name><surname>passwords</surname></customer><customer><id>13</id><name>briant</name><surname>pass</surname></customer><customer><id>14</id><name>frank</name><surname>pass</surname></customer></collection>

这会产生影响吗?

任何帮助将不胜感激。

首先检查ur xml是有效还是无效。 使用xml在线验证器。 如果有效,请尝试此

BufferedReader buffer = new BufferedReader(new InputStreamReader(stream),“ UTF-8”);

似乎是一个问题。 在getXmlFromUrl()中创建InputStreamReader时,应定义正确的字符集。 如果您已经尝试过@omkar的建议,但仍然遇到相同的问题,则应尝试其他编码。 你有没有尝试过:

  • ISO-8859-1
  • US-ASCII
  • UTF-16
  • UTF-16BE
  • UTF-16LE
  • UTF-8

例如:

BufferedReader buffer = new BufferedReader(new InputStreamReader(stream, "ISO-8859-1"));

这些在每个Java实现中均有效。

请注意,您可以控制此XML的创建,那么最好的办法是像这样对DECLARE所使用的字符集进行声明:

<?xml version="1.0" encoding="UTF-8"?>
<collection>
    <customer>
        <id>4</id>
        <name>john</name>
        <surname>test</surname>
    </customer>
</collection>

这样,您可以确定在解析时要使用的字符集。 在上面的示例中,“ UTF-8”。

而且如果一切都失败了,那可能是某些字符的怪异情况,而不是声明的字符集的一部分。

您可以使用不太敏感的解码器,如下所示:

CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
BufferedReader buffer = new BufferedReader(new InputStreamReader(stream, decoder));

它应该只是忽略该错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM