2017-03-02 87 views
0

我試圖解析其具有啓動一些未知字符的xml文件,我得到的錯誤解析有一些未知字符的XML文件在啓動

isjava.lang.IllegalStateException:沒有匹配成功到目前爲止

這裏是xml文件

<?xml version="1.0" encoding="utf-8"?> 
<!--RSS generated by RSSviaXmlTextWriter at Thu, 02 Mar 2017 16:35:42 GMT--> 
<rss version="2.0"> 
    <channel> 
    <title>The Tribune</title> 
    <link>http://www.tribuneindia.com/</link> 
    <description>Tribune News Service</description> 
    <item> 
     <category>Jammu &amp; Kashmir</category> 
     <link>http://www.tribuneindia.com/news/jammu-kashmir/valley-schools-reopen-after-8-months/371227.html</link> 
     <title>Valley schools reopen after 8 months</title> 
     <image>http://images.tribuneindia.com/cms/gall_content/2017/3/2017_3$largeimg01_Wednesday_2017_232657586.jpg</image> 
     <description>SRINAGAR: The schools in the Valley reopened fully today after remaining closed for eight months bringing back the liveliness that had been missing in the winter months. The schools were shut after the eruption of unrest following the killing of Hizbul commander Burhan Wani on July 8 last year.</description> 
     <pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate> 
     <guid>http://www.tribuneindia.com/news/jammu-kashmir/valley-schools-reopen-after-8-months/371227.html</guid> 
    </item> 
    <item> 
     <category>Jammu &amp; Kashmir</category> 
     <link>http://www.tribuneindia.com/news/jammu-kashmir/registration-of-pilgrims-for-amarnath-yatra-begins/371228.html</link> 
     <title>Registration of pilgrims for Amarnath yatra begins</title> 
     <image>http://images.tribuneindia.com/cms/gall_content/2017/3/2017_3$largeimg01_Wednesday_2017_232805408.jpg</image> 
     <description>JAMMU: Amid the chants of 「Bham Bham Bhole」, the registration of pilgrims for this year’s pilgrimage to the Amarnath cave shrine commenced for both Baltal and Chandanwari routes here today.</description> 
     <pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate> 
     <guid>http://www.tribuneindia.com/news/jammu-kashmir/registration-of-pilgrims-for-amarnath-yatra-begins/371228.html</guid> 
    </item> 
    <item> 
     <category>Jammu &amp; Kashmir</category> 
     <link>http://www.tribuneindia.com/news/jammu-kashmir/ladakh-worried-over-costly-air-travel-ahead-of-tourist-season/371235.html</link> 
     <title>Ladakh worried over costly air travel ahead of tourist season</title> 
     <image>http://images.tribuneindia.com/cms/gall_content/2017/3/2017_3$largeimg01_Wednesday_2017_233238395.jpg</image> 
     <description>JAMMU: With Ladakh bracing up to host domestic and foreign tourists, the exorbitant air travel to the arid region continues to be a cause for worry for all stakeholders as the Civil Aviation Ministry is yet to make a formal commitment on 「rationalisation of airfares」 for Ladakh during peak tourist season from May to September.</description> 
     <pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate> 
     <guid>http://www.tribuneindia.com/news/jammu-kashmir/ladakh-worried-over-costly-air-travel-ahead-of-tourist-season/371235.html</guid> 
    </item> 
    <item> 
     <category>Jammu &amp; Kashmir</category> 
     <link>http://www.tribuneindia.com/news/jammu-kashmir/take-steps-for-benefits-of-pilgrims-governor-tells-shrine-board-ceo/371220.html</link> 
     <title>Take steps for benefits of pilgrims, Governor tells Shrine Board CEO</title> 
     <image>http://images.tribuneindia.com/cms/gall_content/2017/3/2017_3$largeimg01_Wednesday_2017_232356489.jpg</image> 
     <description>JAMMU: Governor NN Vohra today said several important issues related to Katra and its surrounding areas were conclusively addressed in the meeting held at Raj Bhawan on February 17 in which Chief Minister Mehbooba Mufti was also present.</description> 
     <pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate> 
     <guid>http://www.tribuneindia.com/news/jammu-kashmir/take-steps-for-benefits-of-pilgrims-governor-tells-shrine-board-ceo/371220.html</guid> 
    </item> 
    <item> 
     <category>Jammu &amp; Kashmir</category> 
     <link>http://www.tribuneindia.com/news/jammu-kashmir/police-find-weapons-in-cross-loc-truck/371225.html</link> 
     <title>Police find weapons in cross-LoC truck</title> 
     <image>http://images.tribuneindia.com/cms/gall_content/archive/</image> 
     <description>SRINAGAR: A cache of weapons, which was being smuggled for militants in the Kashmir valley, was recovered from a truck engaged in cross-LoC trade in north Kashmir’s Baramulla district, the police said today.</description> 
     <pubDate>Thu, 02 Mar 2017 00:57:23 GMT</pubDate> 
     <guid>http://www.tribuneindia.com/news/jammu-kashmir/police-find-weapons-in-cross-loc-truck/371225.html</guid> 
    </item> 
    <item> 
     <category>Jammu &amp; Kashmir</category> 
     <link>http://www.tribuneindia.com/news/jammu-kashmir/shutdown-in-bannihal-town-over-twin-deaths/371579.html</link> 
     <title>Shutdown in Bannihal town over twin deaths</title> 
     <image>http://images.tribuneindia.com/cms/gall_content/archive/</image> 
     <description>JAMMU: A shutdown marred life in a Jammu and Kashmir town on Thursday amid allegations that the driver and a cleaner in a truck found dead in an accident had actually been murdered.</description> 
     <pubDate>Thu, 02 Mar 2017 13:28:31 GMT</pubDate> 
     <guid>http://www.tribuneindia.com/news/jammu-kashmir/shutdown-in-bannihal-town-over-twin-deaths/371579.html</guid> 
    </item> 
    </channel> 
</rss> 

我認爲第l國家統計局2號創建這是

<!--RSS generated by RSSviaXmlTextWriter at Thu, 02 Mar 2017 16:35:42 GMT--> 

明顯的第一行被視爲由xmlpullparser評論的問題,但問題是與第二行。 解析器是我無法解析第二行,因爲它正在搜索的開始標記,它確實遇到字符

這裏是我的解析器

public class SitesXmlPullParserTribuneLocal{ 

    static final String KEY_SITE = "item"; 
    static final String KEY_NAME = "title"; 
    static final String KEY_LINK = "link"; 
    static final String KEY_ABOUT = "description"; 
    static final String KEY_IMAGE_URL = "image"; 
    static final String KEY_DATE = "pubDate"; 
    private static boolean firstCheck = true; 


    public static List<NewsItems> getStackSitesFromFile(Context ctx) { 

     // List of StackSites that we will return 
     List<NewsItems> newsItems; 
     newsItems = new ArrayList<NewsItems>(); 

     // temp holder for current StackSite while parsing 
     NewsItems curNewsItems = null; 


     // Temporary Holder for current text value while parsing 
     String curText = ""; 

     try { 
      // Get our factory and PullParser 
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      XmlPullParser xpp = factory.newPullParser(); 

      // Open up InputStream and Reader of our file. 
      FileInputStream fis = ctx.openFileInput("TribuneLocal.xml"); 
//   Log.e("ERROR at TribuneLocal", String.valueOf(ctx.openFileInput("TribuneLocal.xml"))); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); 

      // point the parser to our file. 
      xpp.setInput(reader); 

      // get initial eventType 
      int eventType = xpp.getEventType(); 
      Log.e("TagName Local", String.valueOf(eventType)); 


      //To get the actual location to start parsing from 
      boolean actual_work = false; 

      // Loop through pull events until we reach END_DOCUMENT 
      Log.e("You Reached To", "Mark1"); 

      while (eventType != XmlPullParser.END_DOCUMENT) { 
       // Get the current tag 
       Log.e("You Reached To", "Mark2"); 
       String tagName = xpp.getName(); 
       Log.e("You Reached To", "Mark3"); 
//    Log.e("TagName is", tagName); 
       // React to different event types appropriately 
       if (eventType != XmlPullParser.START_TAG) { 
        Log.e("EventType Inside", String.valueOf(eventType)); 
//     firstCheck = false; 
        eventType = xpp.next(); 
        continue; 
       } 
       xpp.setFeature("http://xmlpull.org/v1/doc/features.html#relaxed", true); 

       Log.e("EventType Outside", String.valueOf(eventType)); 
       switch (eventType) { 
        case XmlPullParser.START_TAG: 
         Log.e("You Reached To", "Mark4"); 
         if (tagName.equalsIgnoreCase(KEY_SITE)) { 
          // If we are starting a new <news> block we need 
          //a new NewsItems object to represent it 
           actual_work = true; 
           curNewsItems = new NewsItems(); 
         } 

         break; 

        case XmlPullParser.TEXT: 
         //grab the current text so we can use it in END_TAG event 
         curText = xpp.getText(); 
         break; 

        case XmlPullParser.END_TAG: 
         if (tagName.equalsIgnoreCase(KEY_SITE) && actual_work) { 
          // if </item> then we are done with current Site 
          // add it to the list. 
          newsItems.add(curNewsItems); 

         } else if (tagName.equalsIgnoreCase(KEY_NAME) && actual_work) { 
          // if </title> use setTitle() on curSite 
          Log.e("TITLE IS ",curText); 

          curNewsItems.setTitle(curText); 

         } else if (tagName.equalsIgnoreCase(KEY_LINK) && actual_work) { 
          // if </link> use setLink() on curSite 
          Log.e("LINK IS ",curText); 

          curNewsItems.setLink(curText); 
         } else if (tagName.equalsIgnoreCase(KEY_ABOUT) && actual_work) { 
          // if </description> use setDescription() on curSite 
          Log.e("DESCRIPTION IS ",curText); 

          curNewsItems.setDescription(curText); 
         } else if (tagName.equalsIgnoreCase(KEY_DATE) && actual_work) { 
          // if </image> use setImgUrl() on curSite 
          Log.e("DATE IS : ",curText); 

          curNewsItems.setDate(curText); 
         }else if (tagName.equalsIgnoreCase(KEY_IMAGE_URL) && actual_work) { 
          // if </image> use setImgUrl() on curSite 
          Log.e("IMAGE IS : ",curText); 

          curNewsItems.setImgUrl(curText); 
         } 
         break; 

        default: 
         break; 
       } 
       //move on to next iteration 
       eventType = xpp.next(); 
      } 
     } catch (Exception e) { 
      Log.e("Tribune Local File","There is an ERROR Parsing It"); 
     } 

     // return the populated list. 
     return newsItems; 
    } 
} 
+0

您目前*假設*文件的編碼與默認編碼相同。爲什麼要創建一個閱讀器,何時可以將輸入設置爲輸入流?它也沒有幫助,你告訴我們從異常*消息*,但不是堆棧跟蹤... –

+0

第二行沒有問題,這是一個評論! – vm345

+0

@JonSkeet它是堆棧跟蹤中唯一的日誌消息 03-02 19:50:29.509 21723-21723/com.example.raqib.instadate E/exceptiom:isjava.lang.IllegalStateException:目前沒有成功的匹配 03 -02 19:50:29.509 21723-21723/com.example.raqib.instadate E/TagName Local:0 03-02 19:50:29.509 21723-21723/com.example.raqib.instadate E/You Reached To: Mark1 03-02 19:50:29.509 21723-21723/com.example.raqib.instadate E /您已達到:Mark2 03-02 19:50:29.509 21723-21723/com.example.raqib.instadate E /您到達:Mark3 那麼解析器的問題是什麼? –

回答

0

XML文件是確定

此分析該文件,然後你可以使用DOM和提取元素,...

 DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance(); 
     builderFactory.setNamespaceAware(true); 
     DocumentBuilder builder = builderFactory.newDocumentBuilder(); 
     // PARSE 
     Document document = builder.parse(new InputSource(new InputStreamReader(fis))); 
+0

這段代碼是做什麼的? –