SAXParser '&'串联问题 [英] SAXParser '&' concatenation problem

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

问题描述

我目前正在将 SAXParser 与 SAXParserFactory 一起使用,但我遇到了在&"处截断字符串的问题符号.例如:国家创造了我们的世界和其中的一切"变成了其中的一切".

I am presently using SAXParser with SAXParserFactory, and I have run into a problem with strings being cuttoff at '&' symbols. For example: "Nation Created Our World & everything in it" becomes "everything in it".

显然,我不希望这种情况发生.在 xml 输入中,字符被正确转义为 &.我该如何解决?

Obviously, I dont want this to happen. In the xml input, the character is properly escaped as &. How can I resolve this?

try{
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader r = sp.getXMLReader();

            //This handles the xml and populates the entries array
            XMLHandler handler = new XMLHandler();


            // register event handlers
            r.setContentHandler(handler);
            String url = "http://foobar.xml";
            r.parse(url);

            return handler.getEntries();
}

我的 DefaultHandler 类中有这个

I have this in my DefaultHandler class

....
    public void characters( char ch[], int start, int length ){
           String value = new String( ch , start , length );

           if(!value.trim().equals("")) {

               if( currentElement.equalsIgnoreCase("TITLE") ) {
                   tempEntry.setTitle(value);
               }
....

推荐答案

SAX API 不保证任何给定的文本节点都将被一次性交付.允许将其分解为对 characters() 方法的多次调用.您的应用程序必须尽可能适应这种情况,并自行重新组装这些部分.

The SAX API does not guarantee that any given text node will be delivered in one piece. It is permitted to break it up into multiple calls to the characters() method. Your application has to accommodate this possibly, and reassemble the pieces itself.

顺便说一句,Nation 创造了我们的世界 &其中的所有内容 都不是有效的 XML 文本片段,它必须是 Nation Created Our World &里面的一切.在这种情况下,SAX 解析器可能会将其分解为 Nation Created Our World&其中的所有内容,以及您的应用只记得最后一个.

Incidentally, Nation Created Our World & everything in it is not a valid XML text fragment, it would have to be Nation Created Our World & everything in it. In this case the SAX parser may be breaking it up into Nation Created Our World, & and everything in it, and your app is only remembering the last one.

这篇关于SAXParser '&'串联问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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