'&' 的 SAX 解析特点 [英] SAX parsing of '&' character

查看:22
本文介绍了'&' 的 SAX 解析特点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试解析 & 时遇到了 SAX 解析问题char,所有其他特殊字符都在 SAX 解析器中自动解析,但我在 &性格..有人请给我一些建议吗??

I am facing a problem in SAX parsing when I am trying to parse & char, All the other special chars are parsed automatically in SAX parser,but I am facing problem in & character.. anyone pls suggest me something??

首先,我将来自 web 服务的 XML 保存到一个字符串中,并将其并排检查

Firstly I am saving my XML coming from webservices into a string and checking it side by side as

     if(ques_xml.contains("&"))
     {
                ques_xml=ques_xml.replaceAll("&", "&");
    }

//以及我用来解析我保存的XML的以下方法.公共无效 XmlParsing(字符串问题_xml){试试 {

//And the following method I am using to parse my saved XML. public void XmlParsing(String questions_xml) { try {

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXmlHandler myXMLHandler = new MyXmlHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse( new InputSource(new StringReader(questions_xml)));


        } catch (Exception e) {
            String err = (e.getMessage()==null)?"XMLParsing exception":e.getMessage();
            Log.e("XMLParsing Exception",err); 
        }


}

推荐答案

使用SAX解析器解析特殊符号(&)总是有问题,我想只有这件事可以帮助你,处理特殊字符

There are always problem in special symbol (&) parsing using SAX parser, I think this only thing can help you, Handling Special Characters

当您处理包含许多特殊字符的大型 XML 或 HTML 块时,您可以使用 CDATA 部分.CDATA 部分的工作方式类似于 HTML 中的 <code>...</code>,更重要的是:CDATA 部分中的所有空白都很重要,其中的字符不会被解释为 XML.CDATA 部分以 .

When you are handling large blocks of XML or HTML that include many special characters, you can use a CDATA section. A CDATA section works like <code>...</code> in HTML, only more so: all white space in a CDATA section is significant, and characters in it are not interpreted as XML. A CDATA section starts with .

这篇关于'&amp;' 的 SAX 解析特点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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