use*_*849 12 encoding android saxparser xml-parsing apache-stringutils
我有一个字符串,它来自xml,它是德语文本.特定于德语的字符通过UTF-8格式进行编码.在显示我需要解码它的字符串之前.
我尝试过以下方法:
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(
new ByteArrayInputStream(nodevalue.getBytes()), "UTF8"));
event.attributes.put("title", in.readLine());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
try {
event.attributes.put("title", URLDecoder.decode(nodevalue, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
他们都没有工作.如何解码德语字符串
先感谢您.
UDPDATE:
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
if (nodename != null) {
String nodevalue = String.copyValueOf(ch, 0, length);
if (nodename.equals("startdat")) {
if (event.attributes.get("eventid").equals("187")) {
}
}
if (nodename.equals("startscreen")) {
imageaddress = nodevalue;
}
else {
if (nodename.equals("title")) {
// try {
// BufferedReader in = new BufferedReader(
// new InputStreamReader(
// new ByteArrayInputStream(nodevalue.getBytes()), "UTF8"));
// event.attributes.put("title", in.readLine());
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// try {
// event.attributes.put("title",
// URLDecoder.decode(nodevalue, "UTF-8"));
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
event.attributes.put("title", StringEscapeUtils
.unescapeHtml(new String(ch, start, length).trim()));
} else
event.attributes.put(nodename, nodevalue);
}
}
}
Run Code Online (Sandbox Code Playgroud)
rek*_*eru 21
您可以将String构造函数与charset参数一起使用:
try
{
final String s = new String(nodevalue.getBytes(), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
Log.e("utf8", "conversion", e);
}
Run Code Online (Sandbox Code Playgroud)
此外,由于您从xml文档中获取数据,并且我认为它是UTF-8编码的,可能问题在于解析它.
您应该使用InputStream/ InputSource而不是XMLReader实现,因为它带有编码.因此,如果您从http响应中获取此数据,则可以使用InputStream和InputSource
try
{
HttpEntity entity = response.getEntity();
final InputStream in = entity.getContent();
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
Reader reader = new InputStreamReader(in, "UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
parser.parse(is, handler);
//TODO: get the data from your handler
}
catch (final Exception e)
{
Log.e("ParseError", "Error parsing xml", e);
}
Run Code Online (Sandbox Code Playgroud)
或者只是InputStream:
try
{
HttpEntity entity = response.getEntity();
final InputStream in = entity.getContent();
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(in, handler);
//TODO: get the data from your handler
}
catch (final Exception e)
{
Log.e("ParseError", "Error parsing xml", e);
}
Run Code Online (Sandbox Code Playgroud)
更新1
以下是完整请求和响应处理的示例:
try
{
final DefaultHttpClient client = new DefaultHttpClient();
final HttpPost httppost = new HttpPost("http://example.location.com/myxml");
final HttpResponse response = client.execute(httppost);
final HttpEntity entity = response.getEntity();
final InputStream in = entity.getContent();
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(in, handler);
//TODO: get the data from your handler
}
catch (final Exception e)
{
Log.e("ParseError", "Error parsing xml", e);
}
Run Code Online (Sandbox Code Playgroud)
更新2
由于问题不是编码而是源xml被转义为html实体,最好的解决方案是(除了纠正php不要逃避响应),使用apache.commons.lang库非常方便static StringEscapeUtils class.
导入库后,在xml处理程序的characters方法中输入以下内容:
@Override
public void characters(final char[] ch, final int start, final int length)
throws SAXException
{
// This variable will hold the correct unescaped value
final String elementValue = StringEscapeUtils.
unescapeHtml(new String(ch, start, length).trim());
[...]
}
Run Code Online (Sandbox Code Playgroud)
更新3
在您的上一个代码中,问题在于nodevalue变量的初始化.它应该是:
String nodevalue = StringEscapeUtils.unescapeHtml(
new String(ch, start, length).trim());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60449 次 |
| 最近记录: |