的SAX解析'和;“字符 [英] SAX parsing of '&' character

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

问题描述

我现在面临的SAX解析一个问题,当我试图解析和放大器;字符,所有其它特殊字符在SAX解析器自动解析,但我在与放面临的问题;性格..任何人都请提出我的东西?

首先,我救了我的XML从符进入一个字符串,并检查它并排

 如果(ques_xml.contains(&安培;))
     {
                ques_xml = ques_xml.replaceAll(与&,&放大器;放大器;);
    }
 

//而下面的方法,我用解析我保存的XML。     公共无效XmlParsing(字符串questions_xml)     {         尝试{

  / **处理XML * /
        的SAXParserFactory SPF = SAXParserFactory.newInstance();
        的SAXParser藻= spf.newSAXParser();
        XMLReader的XR = sp.getXMLReader();

        / **创建处理程序来处理XML标签(扩展了DefaultHandler)* /
        MyXmlHandler myXMLHandler =新MyXmlHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(新的InputSource(新StringReader(questions_xml)));


        }赶上(例外五){
            字符串ERR =(e.getMessage()== NULL)XMLParsing异常:e.getMessage();?
            Log.e(XMLParsing异常,ERR);
        }


}
 

解决方案

总是有特殊符号的问题(安培)解析使用SAX解析器,我觉得这个唯一可以帮助你,的处理特殊字符

编辑:

当你正在处理XML或HTML大块,其中包括许多特殊字符,你可以使用 CDATA节。 CDATA项工作方式类似于< code> ...< / code> 在HTML中,更多的是:全白在CDATA部分空间是显著,并在它的字符不PTED为XML跨$ P $。 CDATA项开始。

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

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("&", "&");
    }

//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); 
        }


}

解决方案

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

EDIT:

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 .

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

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