2012-05-29 77 views
0

我一直在嘗試使用XmlResourceParser,但我不覺得它是正確的工具。我有一組具有子項目的項目,我想拉出一個具體項目,如在此列表中的第二項:在Android應用程序中使用Java獲取特定的XML標記文本

<story> 
    <id>1</id> 
    <name>The First Room</name> 
    <description>You aren't sure how you ended up here, but there is nothing in this room of interest. You should probably escape.</description> 
    <direction> 
     <name>North</name> 
     <id>2</id> 
    </direction> 
    <direction> 
     <name>East</name> 
     <id>3</id> 
    </direction> 
</story> 
<story> 
    <id>2</id> 
    <name>Moldy Room</name> 
    <description>This room is filled with mold. It would be hazardous to your heath to stick around here.</description> 
    <direction> 
     <name>South</name> 
     <id>1</id> 
    </direction> 
    <direction> 
     <name>West</name> 
     <id>4</id> 
    </direction> 
</story> 

我希望能夠通過「身份證」號碼拉他們總之,無需設置我自己的對象。如果可能的話。

+0

你看我的答案? – AliSh

回答

0

您可以使用此代碼:

 Resources res = activity.getResources(); 
     XmlResourceParser xpp = res 
       .getXml(R.xml.myxml); 
     xpp.next(); 
     int eventType = xpp.getEventType(); 
     eventType = xpp.next(); 
     eventType = xpp.next(); 
     eventType = xpp.next(); 

     short id = Short 
       .parseShort(xpp.getText()); 

     Toast.makeText(
       activity.getApplicationContext(), 
       "" + id, Toast.LENGTH_LONG) 
       .show(); 
+0

這很接近,但我希望能夠使用ID作爲標識符(如主鍵)爲特定的「故事」(可能不是第一個)提取所有數據。 – Organiccat

+1

我最終不得不將它移動到對象中以獲得我想要的結果,但是這回答了基本問題。如果任何人有這樣的未來問題,你將不得不將你的XML解組成對象。 – Organiccat

相關問題