我正在尝试从url使用pullparser解析xml,但是我收到以下错误:
意外的令牌(位置:TEXT - !! .....在java.io.InputStreamReader中)
以下是我用来处理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>
这会产生影响吗?
非常感谢任何帮助。
答案 0 :(得分:1)
首先检查ur xml是有效还是无效。使用xml在线验证器。如果它有效,那么试试这个
BufferedReader buffer = new BufferedReader(new InputStreamReader(stream),“UTF-8”);
答案 1 :(得分:0)
这似乎是一个问题。在getXmlFromUrl()中创建InputStreamReader时,应该定义正确的charset。如果您已经尝试过@omkar的建议但仍然遇到同样的问题,那么您应该尝试其他编码。你试过了吗?
例如:
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>
通过这种方式,您可以确定解析时使用的字符集。在上面的例子中,&#34; UTF-8&#34;。
如果一切都失败了,那可能是某些角色的奇怪情况,而不是声明的charset的一部分。
您可以使用灵敏度较低的解码器,如下所示:
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
BufferedReader buffer = new BufferedReader(new InputStreamReader(stream, decoder));
它应该忽略错误。