简体   繁体   English

非ASCII字符的Java xml解析错误

[英]incorrect java xml parsing for non-ascii characters

I'm using a java applet to browse files/folders on a remote server (ftp/cifs). 我正在使用Java小程序来浏览远程服务器(ftp / cifs)上的文件/文件夹。 The problem is that, if a folder on the remote server is named in non-ascii characters(say Japanese or Chinese language), the applet fails to launch. 问题是,如果远程服务器上的文件夹以非ASCII字符(例如日语或中文)命名,则小程序无法启动。 I've traced back the problem which seems to be arising from incorrect parsing of the non-ascii string in the server response. 我已经追溯了问题,这似乎是由于服务器响应中非ascii字符串的不正确解析引起的。 Following is a snippet of the xml response from the server. 以下是来自服务器的xml响应的摘要。

<?xml version="1.0" encoding="utf-8" ?><a:multistatus xmlns:b="urn:uuid:/" xmlns:c="xml:" xmlns:a="DAV:">
<a:response><a:href>https://xx.xx.xx.xx/folder/äèä¸?能正常工作</a:href><a:propstat><a:status>HTTP/1.1 200 OK</a:status>

The charset encoding from server is requested to be utf-8 as above. 如上所述,要求服务器提供的字符集编码为utf-8。 The garbage looking string is the remote webfolder I'm trying to view using the applet, the folder is actually named 不能正常工作. 查找垃圾的字符串是我试图使用小程序查看的远程Web文件夹,该文件夹实际上名为不能正常工作。 Since the two are not equivalent (actual folder name and the one in the xml), java xml parsing throws an error while trying to launch the applet. 由于两者不相等(实际的文件夹名称和xml中的一个),因此在尝试启动applet时,java xml解析会引发错误。 Please suggest what could be done to fix this. 请提出可以解决此问题的方法。 Also I'm not using the SAX parser, I'm using the XML parser and then create a DOM object 另外,我不使用SAX解析器,而是使用XML解析器,然后创建一个DOM对象。

The XML you have posted seems to contain the garbage characters. 您发布的XML似乎包含垃圾字符。 I mean that those unicode characters have been mangled while creation itself. 我的意思是那些unicode字符在创建本身时就被破坏了。 So the first thing to check should be to make sure that you use right variant of APIs which takes charset as a parameter and use UTF-8 as the parameter in those places.On the server as well as the client se just double check the APIs. 因此,首先要检查的是确保使用正确的API变体,该变体以charset作为参数,并在这些位置使用UTF-8作为参数。在服务器和客户端上,只需仔细检查API 。 For example if you are creating InputStreamReader anywhere then make sure that you are using InputStreamReader(InputStream in, Charset cs) etc. 例如,如果要在任何地方创建InputStreamReader,请确保使用InputStreamReader(InputStream in,Charset cs)等。

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

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