xml 解析包含空格的单个字符串
我的 Android 应用程序正在调用 .NET Web 方法,它返回一个字符串。我正在使用 saxparser 来解析单个字符串,例如:
<string xmlns="http://tempuri.org/">LOCAL NT AUTHORITY\NTLM Authentication Not in role Administrators</string>
和数据处理程序中的 strings() 函数:
public void characters(char ch[], int start, int length) {
String chars = new String(ch, start, length);
Log.v("characters", chars);
chars = chars.trim();
if (_inItem) {
_data = chars;
}
}
我的问题是,当我使用网络服务器 1 测试它时,它工作正常,但使用网络服务器 2 时它返回空字符串。
在网络服务器 1 中,我看到 strings() 函数仅被调用一次,它返回整个字符串“LOCAL NT AUTHORITY\NTLM Authentication Not in role Administrators”。完美的!
但在网络服务器 2 中,charaters() 函数被多次调用,最终返回空字符串..?
04-20 10:09:43.161: VERBOSE/characters(405): LOCAL
04-20 10:09:43.161: VERBOSE/characters(405): NT AUTHORITY\NTLM Authentication
04-20 10:09:43.161: VERBOSE/characters(405): Not in role Administrators
04-20 10:09:43.250: VERBOSE/endElement(405): string=0
与处理空间有关吗?这里发生了什么事?
我最终改变了characters()函数:
public void characters(char ch[], int start, int length) {
String chars = new String(ch, start, length);
if (_inItem) {
_data += chars;
}
}
My Android app is calling .NET web method and it returns a single string. I am using saxparser to parse a single string like:
<string xmlns="http://tempuri.org/">LOCAL NT AUTHORITY\NTLM Authentication Not in role Administrators</string>
and characters() function in my data handler:
public void characters(char ch[], int start, int length) {
String chars = new String(ch, start, length);
Log.v("characters", chars);
chars = chars.trim();
if (_inItem) {
_data = chars;
}
}
My problem is that when I test it with webserver 1, it works ok but with webserver 2 it returns empty string.
With webserver 1, I saw characters() function is being called just once and it returns the whole string "LOCAL NT AUTHORITY\NTLM Authentication Not in role Administrators". Perfect!
But with webserver 2, charaters() function is being called multiple times and it finally returns empty string..?
04-20 10:09:43.161: VERBOSE/characters(405): LOCAL
04-20 10:09:43.161: VERBOSE/characters(405): NT AUTHORITY\NTLM Authentication
04-20 10:09:43.161: VERBOSE/characters(405): Not in role Administrators
04-20 10:09:43.250: VERBOSE/endElement(405): string=0
Is it about handling spaces? What's happening here?
I ended up changing characters() function:
public void characters(char ch[], int start, int length) {
String chars = new String(ch, start, length);
if (_inItem) {
_data += chars;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是 sax 接口的定义方式,可以对字符进行多次调用,不希望您在一次调用中获得单个元素中的所有字符。如果您需要将整个字符串整合在一起,则由您的实现来收集并将它们连接在一起。
This is how the sax interface is defined, there can be multiple calls to characters, its not expected that you'll get all the characters in a single element in one call. If you need the entire string in one piece, its up to your implementation to collect and concatenate them together.