Android的SAX解析器与换行符样? [英] Android SAX Parser with newline character sample?

查看:183
本文介绍了Android的SAX解析器与换行符样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP Web服务返回的XML输出​​,标签之一,包含多行\\ n字符串数据。

I have a PHP webservice that returns an XML output, and one of the Tag contains a multiline "\n" String data.

我的问题是,所有的\\ n是我通过默认SAX解析器将XML数据后被删除,但我真的不希望这种事情发生!我希望将所有换行符\\ n字
标签内,这样我可以提取它,并在一个简单的TextView或EditText上显示出来。

My problem is, all "\n" is being removed after I pass the XML data through the default SAX parser, but I really dont want this to happen! I want to retain all newline "\n" characters within the Tag so that I can extract it and display them in a simple TextView or EditText.

我试过一对夫妇像取代\\ n变通办法&放大器; X#达;或什么的,但我不能把它与我的PHP服务器正常工作,而且我觉得它不是一个完美的解决方案。

I tried a couple of workarounds like substituting the "\n" with "&x#dA;" or something, but I cant get it to work properly with my PHP server, and I feel its not an elegant solution.

有没有人有一个工作的例子或教程你可以点我来学习如何解决这个问题?我读到这里,这是thsi默认SAX解析器实施的lmitation另一篇文章,但我使用的是第三部分的解析器如果那是要解决它不介意,但我会需要一些指导...

Does anyone have a working example or tutorial you can point me to learn how to resolve this? I read in another post here that this is the "lmitation" of the implementation of thsi default SAX parser, but I dont mind using a third part parser if thats gonna solve it, but I will need some guidance...

谢谢!

推荐答案

我有同样的问题,因为你。我发现<一个href=\"http://stackoverflow.com/questions/1234510/how-do-i-replace-a-character-in-a-string-in-java/1234531#1234531\">this解决方案,以类似的问题,它适应了这一点。

I had the same problem as you. I found this solution to a similar problem and adapted it to this one.

StringBuffer sb = new StringBuffer(str.length());

CharacterIterator it = new StringCharacterIterator(str);
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
    switch (ch) {
        case '\\':
            char next = str.charAt(it.getIndex() + 1);
            if (next == 'n') {
                // we've found a newline character
                sb.append('\n');
            } else {
                sb.append(ch);
            }
            break;
        case 'n':
            char prev = str.charAt(it.getIndex() - 1);
            if (prev == '\\') {
                // don't append the 'n', we did that above
            } else {
                sb.append(ch);
            }
            break;
        default:
            sb.append(ch);
            break;
    }
}

str = sb.toString();

其中, STR 是字符串 \\ n 字符。有可能是一个更优雅的解决这个问题,但我很新的Java和Android和找不到的。

Where str is your string with \n characters. There is probably a more elegant solution to this problem, but I am quite new to java and Android and couldn't find one.

这篇关于Android的SAX解析器与换行符样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆