XmlPullParser exception - can't work it out

99封情书 提交于 2019-12-24 01:19:30

问题


I have a basic method - ReadXML() - that reads in an xml file from the assets folder. When this runs no exceptions are thrown. But from within this method I call a doXML() method that tries to extract text from the xml file and set this text into textviews. I keep getting the following error when the doXML() method runs...The error that is generated is

18:19:41.654: W/System.err(329): org.xmlpull.v1.XmlPullParserException: name expected (position:START_TAG @59:57 in java.io.InputStreamReader@44f049c8).

I tried to shorten the xml file to see where the error is...essentially if there are four "trials" each with a title, author, etc. the first three will actually load into the textviews...although the above error is still generated in LogCat...but the last one won't load...so there's probably an issue with the code logic but can't figure it out.

thanks in advance.

<trial>
<title>The Book</title>
<author></author>
....
</trial>

I think the code is probably more complicated than it needs to be...but can't figure out why the doXML() method won't run properly.

public void ReadXML() throws XmlPullParserException, IOException
   {

      factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(false);
      xpp = factory.newPullParser();

      InputStream raw = getApplicationContext().getAssets().open("trial_info.xml");
      xpp.setInput(raw,null);
   }
}


public void doXML() throws  XmlPullParserException, IOException{


          int eventType = XmlPullParser.START_TAG;

          while(eventType != XmlPullParser.END_DOCUMENT)
          {
            if(eventType == XmlPullParser.TEXT && xpp.getText().equals(selectedTrial))
              {
                   trialTxt = (TextView)findViewById(R.id.TrialAbbrevData);
                   trialTxt.setText(xpp.getText());
                   Log.d("what is this", xpp.getText());

           while(!(eventType == XmlPullParser.END_TAG && xpp.getName().equals("...")))
                   {  

          if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("trialName"))
                      {    
                         eventType = xpp.next();
                         if(eventType == XmlPullParser.TEXT)
                         {
                            trialTxtDesc = (TextView)findViewById(R.id.TrialNameData);
                            trialTxtDesc.setText(xpp.getText());
                            Log.d("in loop",xpp.getText());
                         }
                      }

       if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("trialDetails"))
                      {   
                         eventType = xpp.next();
                         if(eventType == XmlPullParser.TEXT)
                         {
                            trialTxtConc = (TextView)findViewById(R.id.TrialDescData);
                            trialTxtConc.setText(xpp.getText());
                         }
                      }

       if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("trialResults"))
                      {   
                         eventType = xpp.next();
                         if(eventType == XmlPullParser.TEXT)
                         {
                     trialTxtConc = (TextView)findViewById(R.id.TrialResultsData);
                            trialTxtConc.setText(xpp.getText());
                         }
                      }

   if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("trialConclusion"))
                      {   
                         eventType = xpp.next();
                         if(eventType == XmlPullParser.TEXT)
                         {
                            trialTxtConc=(TextView)findViewById(R.id.TrialConclusionData);
                            trialTxtConc.setText(xpp.getText());
                         }
                      }

        if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("whatItMeans"))
                      {  
                          eventType = xpp.next();
                          if(eventType == XmlPullParser.TEXT)
                          {
                             trialTxtMeans = (TextView)findViewById(R.id.TrialWIMData);
                             trialTxtMeans.setText(xpp.getText());
                          }
                      }

                      eventType = xpp.next();

                   }         



            }
              eventType = xpp.next();
        }
    }

来源:https://stackoverflow.com/questions/11076645/xmlpullparser-exception-cant-work-it-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!