2012-07-13 70 views
0

我正在使用一個SAX解析器來處理我正在處理的一個android應用程序,並且我很難嘗試保存項目的名稱標記被覆蓋。這裏的問題是由於標籤是相同的,因此項目中的我的名稱標籤被許可證中的名稱標籤覆蓋。剩下的就是美麗的解析。另外,這個xml是通過將GET請求傳遞給API來生成的,所以我不能編輯它。有沒有辦法可以省略許可證字段中的名稱標籤,或者嘗試瞭解我在文檔中的位置,這是一個問題,因爲SAX甚至被驅動。另外,我意識到我可以很容易地使用DOM來完成這一任務,但現在我已經開始轉向DOM了。我附加了我的XML處理代碼片段和我試圖解析的xml文件。SAX解析器相同的標記覆蓋值

感謝

@Override 
public void startElement (String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException 
{ 
    buffer.setLength(0); 

    if (localName.equalsIgnoreCase("result")) 
    { 
     results = new ArrayList <Result>(); 
    } 

    if (localName.equalsIgnoreCase("project")) 
    { 
     result = new Result(); 
    } 

    if (localName.equalsIgnoreCase("name")) 
    { 
     result.name = buffer.toString(); 
     Log.v("PROJECT NAME: ", result.name); 
    } 
} // end of startElement() 

@Override 
public void endDocument() throws SAXException 
{} 

@Override 
public void endElement (String namespaceURI, String localName, String qName) throws SAXException 
{  
    if (localName.equalsIgnoreCase("project") || localName.equalsIgnoreCase("account")) 
    { 
      this.entries++; 
      results.add(result); 
    } 

    else if (localName.equalsIgnoreCase("id")) 
    { 
     result.id = buffer.toString(); 
     Log.v("Id: ", result.id); 
    } 

    else if (localName.equalsIgnoreCase("created_at")) 
    { 
     try 
     { 
      result.date_created = ResultHandlerProject.DATE_FORMAT.parse(buffer.toString()); 
     } 
     catch (ParseException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    else if (localName.equalsIgnoreCase("updated_at")) 
    { 
     try 
     { 
      result.date_updated = ResultHandlerProject.DATE_FORMAT.parse(buffer.toString()); 
     } 
     catch (ParseException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    else if (localName.equalsIgnoreCase("description")) 
    { 
     result.description = buffer.toString(); 
    } 

    else if (localName.equalsIgnoreCase("homepage_url")) 
    { 
     result.url_homepage = buffer.toString(); 
    } 

    else if (localName.equalsIgnoreCase("download_url")) 
    { 
     result.url_download = buffer.toString(); 
    } 

    else if (localName.equalsIgnoreCase("medium_logo_url")) 
    { 
     result.image_link = buffer.toString(); 
    } 

    else if (localName.equalsIgnoreCase("user_count")) 
    { 
     result.user_count = buffer.toString(); 
    } 

    else if (localName.equalsIgnoreCase("average_rating")) 
    { 
     result.rating = buffer.toString(); 
    } 

    else if (localName.equalsIgnoreCase("rating_count")) 
    { 
     result.rating_count = buffer.toString(); 
    } 

    else if (localName.equalsIgnoreCase("analysis_id")) 
    { 
     result.analysis_id = buffer.toString(); 
    } 

    else if (localName.equalsIgnoreCase("nice_name")) 
    { 
     result.license_full_name = buffer.toString(); 
     Log.v("License Full Name: ", result.license_full_name); 
    } 

} // end of endElement() 

示例XML文件:

<result> 
<project> 
<id>6050</id> 
<name>FirePHP</name> 
<created_at>2007-06-16T04:03:13Z</created_at> 
<updated_at>2012-07-01T15:05:23Z</updated_at> 
<description> 
FirePHP 
<homepage_url>http://www.firephp.org/</homepage_url> 
<download_url>http://www.firephp.org/</download_url> 
<url_name>FirePHP</url_name> 
<medium_logo_url> 
https://s3.amazonaws.com/cloud.ohloh.net/attachments/6638/FirePHP_Large_White_med.png 
</medium_logo_url> 
<small_logo_url> 
https://s3.amazonaws.com/cloud.ohloh.net/attachments/6638/FirePHP_Large_White_small.png 
</small_logo_url> 
<user_count>145</user_count> 
<average_rating>4.11765</average_rating> 
<rating_count>34</rating_count> 
<analysis_id>8910317</analysis_id> 
<licenses> 
<license> 
<name>bsd</name> 
<nice_name>BSD Copyright</nice_name> 
</license> 
</licenses> 
</project> 
+0

對不起,我沒有讓你懷疑......「name」和「nice_name」標籤正在被覆蓋。這是你的問題嗎? – yugidroid 2012-07-13 18:25:55

+0

我的結果/項目/名稱被許可證/許可證/名稱覆蓋 因此,在這裏,最初爲'FirePHP'的結果/項目/名稱將被我的許可證/許可證/名稱覆蓋,因此會變成'bsd' – Pulkit2692 2012-07-13 19:39:24

+0

請看下面的答案。 – yugidroid 2012-07-13 23:57:21

回答

0

您可以用簡單的方式解決問題。

  1. 在您的類處理程序中創建一些標誌。

    private boolean inProject = false; 
    private boolean inLicense = false; 
    
  2. 控制這些標誌就像我告訴你以下。

    @Override 
        public void startElement (String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException 
        { 
         if (localName.equalsIgnoreCase("project")) { 
          this.inProject = true; 
         } 
         else if (localName.equalsIgnoreCase("license")) { 
          this.inLicense = true; 
         } 
        } 
    
        @Override 
        public void endElement (String namespaceURI, String localName, String qName) throws SAXException 
        {  
         if (localName.equalsIgnoreCase("name") { 
          if(inProject) { 
           //Set your project name here. 
           this.inProject = false; 
          } 
          else if (inLicense) { 
           //Set your lencense name here. 
           this.inLicense = false; 
          } 
         } 
        } 
    

讓我知道,如果它的工作原理。

+0

非常感謝,夥計。一個非常簡單和優雅的解決方案我猜想我正在以一種複雜的方式來思考它,並且正在放棄sax for dom或使用XPath。但是,謝謝你,我現在不必經歷這個麻煩。欣賞它。 – Pulkit2692 2012-07-14 04:51:59