dongzhao5970 2011-04-10 17:39
浏览 60
已采纳

带有换行符字符样本的Android SAX Parser?

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

My problem is, all " " 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 " " characters within the Tag so that I can extract it and display them in a simple TextView or EditText.

I tried a couple of workarounds like substituting the " " 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.

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...

Thanks!

  • 写回答

1条回答 默认 最新

  • duandaotuo5542 2011-05-02 17:04
    关注

    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('
    ');
                } 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();
    

    Where str is your string with 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码