2012-02-29 34 views
0

我沒有經驗的XML解析,所以也許我寫的東西看起來愚蠢的一些,也許我的一些術語不太正確..請原諒。解析從互聯網xml(yr.no)

我開發了一個android應用程序,它需要其他人來解析來自YR.no的天氣數據。該組織提供了一個提供某些xml格式數據的方法。比方說,比如我想從這個http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad

我開發了一個代碼,可以做一些XML解析解析XML數據,並在這座http://www.w3schools.com/xml/simple.xml工作(作爲測試)。

主要行代碼來定義我的BaseFeedParser類得到什麼是:

RootElement root2 = new RootElement("breakfast_menu"); 
Element food = root2.getChild("food"); 
Element name = food.getChild("name"); 

food.setEndElementListener(new EndElementListener() { 
    public void end() { 
     messages.add(currentMessage.copy()); 
    } 
}); 

food.getChild("name").setEndTextElementListener(new EndTextElementListener() { 
    public void end(String body) { 
     currentMessage.setTitle(body); 
    } 
}); 

try { 
    Xml.parse(this.getInputStream(), Xml.Encoding.ISO_8859_1, root2.getContentHandler()); 
} catch (Exception e) { 
    throw new RuntimeException(e); 
} 
return messages; 

然後從我的活動類:

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    loadFeed(); 
} 

private void loadFeed() { 
    try { 
     BaseFeedParser parser = new BaseFeedParser(); 
     messages = parser.parse(); 
     List<String> titles = new ArrayList<String>(messages.size()); 
     System.out.println(messages.size()); 
     for (Message msg : messages) { 
      titles.add(msg.getTitle()); 
     } 
     ArrayAdapter<String> adapter = 
       new ArrayAdapter<String>(this, R.layout.row,titles); 
     this.setListAdapter(adapter); 
     String str = "!"; 
     if (titles != null) { 
      str = titles.toString(); 
      System.out.println("not null"); 
      System.out.println(str); 
     } 

     test(str); 
    } catch (Throwable t) { 
     Log.e("AndroidNews",t.getMessage(), t); 
    } 
} 

public void test(String s) { 
    setContentView(R.layout.error); 
    TextView textView = (TextView) findViewById(R.id.mytextview); 
    textView.setText(s); 
} 

那麼它將返回並打印我想要的數據( 「比利時華夫餅」等)

我最初想解析的yr.no數據的問題是,每個結束的孩子不包含一個值,但可以有更多的標籤(例如<waveDirection unit="degree" value="250"/>)。所以當我改變元素使用這個元素時,它會結束到25個不同的String s(如果您計數的是所有不同的子標籤waveDirection),但每個值都是空的(如String a = "")。我沒有錯誤,我只是得到了25個空字符串的列表。我試圖達到我的元素的方法是這樣的:

RootElement root = new RootElement("weatherdata"); 
Element product = root.getChild("product"); 
Element time = product.getChild("time"); 
Element location = time.getChild("location"); 

location .setEndElementListener(new EndElementListener(){ 
public void end() { 
     messages.add(currentMessage.copy());    
    } 
}); 

location.getChild("windDirection").setEndTextElementListener(new EndTextElementListener() { 
    public void end(String body) { 
     currentMessage.setTitle(body); 
    } 
}); 

所以我應該怎麼修改此使其與該XML的作品?我不提供所有的類和方法(如setTitle()),但我認爲他們工作,因爲他們解析我的第一個測試XML。我想我設置我的feedUrlString = "http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad";,因爲它找到了文檔的根目錄和25個元素。

編輯:我做到了!獲得屬性的正確方法是使用:

location.setStartElementListener(new StartElementListener(){ 
     public void start(Attributes attributes){ 
      messages.add(currentMessage.copy());    
     } 
    }); 

    location.getChild("windDirection").setTextElementListener(new TextElementListener(){ 
    public void end(String body) { 
     //currentMessage.setTitle(body); 
     //System.out.println("b="+ body); 
     } 

    @Override 
    public void start(Attributes attributes) { 
      System.out.println("val" + attributes.getValue("deg")); 
      currentMessage.setTitle(attributes.getValue("deg")); 
     } 
    }); 

所以,現在我得到我的數據,但由於某種原因,所有除了最後一個元素(我測試了其他YR.no個XML以及)。有必須是我應該解決的一些錯誤,但重要的一步已經完成。謝謝大家的答案,特別是user306848誰指出我使用的方向!

回答

1

我覺得你的代碼假定存在是代表你所尋求的價值文本節點。對於yr.no域中的xml文件,情況並非如此。

您需要弄清楚如何從您使用的xml庫的標籤中讀取屬性。

我沒有android開發經驗,但是你使用android.sax包嗎?那麼我認爲android.sax.StartElementListener將是一個很好的候選人。它接收屬於特定標籤的屬性。

+0

謝謝,我使用這個方向工作(見編輯)! – george 2012-03-01 09:44:09

2

使用DOM解析器.......這將是容易...

看到一些教程從這裏,

http://www.roseindia.net/xml/dom/

+0

這看起來很有希望thanx!我會對這個教程做一些研究並告訴你! – george 2012-02-29 14:56:19

+0

Ohk sure .......你歡迎... :) – 2012-02-29 17:37:52

1

使用this example

他正在使用本地存儲的XML文件。如果你得到一個XML飼料的代碼更改爲以下:

URL url = new URL("ENTER XML LINK"); 
    InputStream stream = url.openStream(); 
    Document doc = docBuilder.parse(stream); 
+0

謝謝你的回答!但是這顯示瞭如何獲得簡單的xml,就像我測試的那樣(我可以很容易地用教程的代碼進行解析,並且它工作正常)。但是我不能在xml上使用它,我實際上想從 – george 2012-02-29 14:58:27

+0

中解析。你想從WaveDirection和其他人的單位和價值,但是當你解析它的不顯示? – 2012-02-29 15:21:58

+0

用我原來的代碼顯示一個空字符串..與此我直到現在不能形成它(我形成它顯示第一個xml好,但不能爲另一個設置孩子..) – george 2012-02-29 17:48:51