为什么我在下面给出的代码中得到SAXException

时间:2012-09-07 14:36:55

标签: android

为什么我在下面给出的代码中得到SAXException。 XML文件处理程序和logcat如下所示。处理程序是否有任何问题。这个错误是因为XML如下所示。

响应:

1    
2   <xml version="1.0">
3   <xml>
4   <opcode>
5       <tsip>65.99.254.23:3939</tsip> | 
6       <ssip>207.210.233.180:5060</ssip> |
7       <brand>iVoip Dialer</brand> |
8       <balance>http://207.210.233.180:8888/cdr/get_balance.php?pin=</balance> |
9       <rates>www.democall.com/ratelink</rates> |
10      <domain>www.sabavoip.com</domain> |
11      <operator>0786</operator> |
12      <active>Active</active>
13  </opcode>
14  </xml>
15
16

代码:

try {
            HttpClient client = new DefaultHttpClient();
            HttpGet method= new HttpGet();
            method.setURI(new URI(AppConstatnt.OPCODE_URL+code));

            HttpResponse response=client.execute(method);           
            HttpEntity entity=response.getEntity();         
            InputStream stream=entity.getContent();

            Log.e("b4 execute in login","aditya");

            SAXParserFactory spf = SAXParserFactory.newInstance(); 
            SAXParser sp = spf.newSAXParser(); 
            XMLReader xr = sp.getXMLReader(); 
            OPCodeHandler myopcodeHandler = new OPCodeHandler();
            xr.setContentHandler(myopcodeHandler); 

            xr.parse(new InputSource(stream)); 
            objsitelist = myopcodeHandler.getSiteList();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }

处理程序:

public class OPCodeHandler extends DefaultHandler {

    boolean currentElement = false;
    String currentValue = "";
    SitesList siteobject;

    public SitesList getSiteList() {
        return siteobject;
    }

    public void setSiteList(SitesList objsite) {
        siteobject = objsite;
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        currentElement = true;
        currentValue = "";
        if (localName.equalsIgnoreCase("opcode")) {
            siteobject = new SitesList();
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        currentElement = false;
        if (localName.equalsIgnoreCase("tsip")) {
            siteobject.setTsip(currentValue);
        }
        if (localName.equalsIgnoreCase("ssip")) {
            siteobject.setSsip(currentValue);
        }
        if (localName.equalsIgnoreCase("brand")) {
            siteobject.setBrand(currentValue);
        }
        if (localName.equalsIgnoreCase("balance")) {
            siteobject.setBalance(currentValue);
        }
        if (localName.equalsIgnoreCase("rates")) {
            siteobject.setRates(currentValue);
        }

        if (localName.equalsIgnoreCase("domain")) {
            siteobject.setDomain(currentValue);
        }
        if (localName.equalsIgnoreCase("operator")) {
            siteobject.setOperator(currentValue);
        }
        if (localName.equalsIgnoreCase("active")) {
            siteobject.setActive(currentValue);
        }
        if (localName.equalsIgnoreCase("opcode")) {
            setSiteList(siteobject);
        }

    }

}

logcat的:

org.apache.harmony.xml.ExpatParser$ParseException: At line 16, column 0: no element found
org.apache.harmony.xml.ExpatParser.finish(ExpatParser.java:550)
org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:480)

org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)

org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)

com.ievatech.webservice.AppRequestHandler.opCodeRequest(AppRequestHandler.java:53)

com.sample.test.SampleAppActivity$Communicator.doInBackground(SampleAppActivity.java:131)

com.sample.test.SampleAppActivity$Communicator.doInBackground(SampleAppActivity.java:1)
android.os.AsyncTask$2.call(AsyncTask.java:185)
09-07 19:58:36.439: W/System.err(9731):     
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
java.util.concurrent.FutureTask.run(FutureTask.java:138)

java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)

java.lang.Thread.run(Thread.java:1019)

0 个答案:

没有答案