简体   繁体   English

在Android中使用SAX解析器解析XML基本Web服务的问题

[英]issue in parsing the xml base webservice using sax parser in android

I have developed simple webservice base math app in which I use the XML base service and use sax parser to parse the webservice response. 我已经开发了简单的基于webservice数学应用程序,其中使用了XML基本服务并使用了sax parser来解析webservice响应。

I already parse the XML base webservice using sax parser but my problem is when I parse this not get the whole data of the particular tag . 我已经使用sax解析器解析了基于XML基础webservice但是我的问题是当我解析时无法获得特定tag的全部数据。 For this my code is below. 为此,我的代码如下。

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

Problem is language Name Tag which contain very big string of data like 问题是language名称标签包含很大的数据字符串,例如

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

When I parse this it can`t get the proper string value of this tag how can I get this? 当我解析它时,它无法获得该标签的正确字符串值,我该如何获得它呢?

If you special characters such as apostrophes and others in your response the data will break at that particular point. 如果您在响应中使用特殊字符(例如撇号和其他字符),则数据将在该特定点中断。 You can read here for more SAX parser: Ignoring special characters 您可以在这里阅读更多SAX解析器:忽略特殊字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM