在android中使用sax解析器解析xml基础web服务的问题

时间:2012-10-04 05:07:59

标签: android saxparser

我开发了简单的webservice基础数学应用,我在其中使用XML基本服务并使用sax parser来解析webservice响应。

我已经使用sax解析器解析了XML base webservice但我的问题是当我解析它时没有得到特定tag的整个数据。为此,我的代码如下。

private List < Object > callWebService() {
 try {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser parser = factory.newSAXParser();

  final List < Object > list = new ArrayList < Object > ();

  DefaultHandler handler = new DefaultHandler() {
   boolean language = false;
   boolean langid = false;
   boolean langname = false;
   boolean icon = false;

   public void startDocument() throws SAXException {
    list.clear();
   }

   public void endDocument() throws SAXException {}

   public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if (qName.equalsIgnoreCase("language")) {
     language = true;
     objLanguage = new Language();
     return;
    }

    if (qName.equalsIgnoreCase("langid")) {
     langid = true;
     return;
    }

    if (qName.equalsIgnoreCase("langname")) {
     langname = true;
     return;
    }
    if (qName.equalsIgnoreCase("icon")) {
     icon = true;
     return;
    }
   }

   public void endElement(String uri, String localName, String qName) throws SAXException {
    if (qName.equalsIgnoreCase("langid")) {
     langid = false;
     return;
    }

    if (qName.equalsIgnoreCase("langname")) {
     langname = false;
     return;
    }
    if (qName.equalsIgnoreCase("icon")) {
     icon = false;
     return;
    }

    if (qName.equalsIgnoreCase("language")) {
     if (langid == false && langname == false && icon == false) {
      language = false;
      list.add(objLanguage);
      return;
     }
    }
   }

   public void characters(char ch[], int start, int length) throws SAXException {
    String theString = new String(ch, start, length);

    if (langid == true) {
     objLanguage.setLangId(theString);
    } else if (langname == true) {
     objLanguage.setLangName(theString);
    } else if (icon == true) {
     objLanguage.setIconImage(genHelper.convertIconImageInByteBuffer(theString));
    }
   }
  };

  parser.parse("http://mathevaluate.com/webservice/langlist.php", handler);
  return list;
 } catch (Exception ex) {
  System.out.println("ERROR : " + ex.toString());
  return null;
 }
}

问题是language名称标签,其中包含非常大的数据字符串,如

<langname>this is the part where  i can`t get the whole string of the tag so post this question</langname>

当我解析它时,它无法获得此标记的正确字符串值,我怎么能得到它?

1 个答案:

答案 0 :(得分:0)

如果您的回复中有特殊字符(如撇号和其他字符),则数据将在该特定点处中断。您可以在此处阅读更多SAX parser: Ignoring special characters