麻烦在Android API 1.5解析与SAX解析器(javax.xml.parsers.SAXParser中)报价 [英] Trouble parsing quotes with SAX parser (javax.xml.parsers.SAXParser) on Android API 1.5

查看:95
本文介绍了麻烦在Android API 1.5解析与SAX解析器(javax.xml.parsers.SAXParser中)报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用SAX解析器解析时,有一个节点中的内容。我怎样才能解决这个我需要转换吗?字符失败?

在换句话说,只要我有一个节点报价:

 <节点>在节点包含引号&LT字符; /节点>

当处理程序是分析它这个节点被宰割成多个字符数组。这是正常的行为呢?行情为什么会出现这样的问题?

下面是code我使用的:

 进口javax.xml.parsers.SAXParser中;
进口javax.xml.parsers.SAXParserFactory中;
进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.methods.HttpGet;
进口org.xml.sax.InputSource中;
进口org.xml.sax.XMLReader中; ...
HTTPGET HTTPGET =新HTTPGET(GATEWAY_URL +/+ question.getId());
          httpget.setHeader(用户代理,PayloadService.userAgent);
          httpget.setHeader(内容类型,应用程序/ XML);          。HTT presponse响应= PayloadService.getHttpclient()执行(HTTPGET);
          HttpEntity实体= response.getEntity();          如果(实体!= NULL)
          {
              SPF SAXParserFactory的= SAXParserFactory.newInstance();
              的SAXParser SP = spf.newSAXParser();
              XMLReader的XR = sp.getXMLReader();              ConvoHandler convoHandler =新ConvoHandler();
              xr.setContentHandler(convoHandler);
              xr.parse(新的InputSource(entity.getContent()));
              entity.consumeContent();               给messageManager = convoHandler.getMessageList();
          }


解决方案

该错误是在你最近的评论中引用您的处理程序类。

在写一个ContentHandler中的一个常见的​​错误是假设字符的方法,只是要与所有的字符数据调用一次。它其实是可以与字符的数据块,你必须收集被多次调用。斩波成多个字符数组是正常的行为。

也许你需要在你的startElement方法来启动一个收集器(也许一个StringBuffer),在你的endElement方法收集到的数据在你的人物的方法,然后使用数据,这应该是一个在您的评论显示的message.setText被调用。

When using a SAX parser, parsing fails when there is a " in the node content. How can I resolve this? Do I need to convert all " characters?

In other words, anytime I have a quote in a node:

 <node>characters in node containing "quotes"</node>

That node gets butchered into multiple character arrays when the Handler is parsing it. Is this normal behaviour? Why should quotes cause such a problem?

Here is the code I am using:

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

 ...


HttpGet httpget = new HttpGet(GATEWAY_URL + "/"+ question.getId());
          httpget.setHeader("User-Agent", PayloadService.userAgent);
          httpget.setHeader("Content-Type", "application/xml");

          HttpResponse response = PayloadService.getHttpclient().execute(httpget);
          HttpEntity entity = response.getEntity();

          if(entity != null)
          {        
              SAXParserFactory spf = SAXParserFactory.newInstance();
              SAXParser sp = spf.newSAXParser();            
              XMLReader xr = sp.getXMLReader();            

              ConvoHandler convoHandler = new ConvoHandler();
              xr.setContentHandler(convoHandler);             
              xr.parse(new InputSource(entity.getContent()));                                


              entity.consumeContent();         

               messageList = convoHandler.getMessageList();


          }

解决方案

The error is in your handler class referenced in your most recent comment.

A common error in writing a ContentHandler is to assume the characters method is only going to be called once with all the character data. It can in fact be called multiple times with chunks of the character data, which you have to collect. The chopping up into multiple character arrays is normal behavior.

Probably you need to initiate a collector (maybe a StringBuffer) in your startElement method, collect data into it in your characters method and then use the data in your endElement method, which should be where the message.setText shown in your comment is called.

这篇关于麻烦在Android API 1.5解析与SAX解析器(javax.xml.parsers.SAXParser中)报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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