2013-01-06 154 views
0

我需要我的應用程序中的關係數據,其數據將從兩個XML文件中加載,這些文件將存儲在assets文件夾中或從Server下載。Android解析XML

categories(id, name, slug) // using a slug string to select the category icon drawable in list view 
places(id, name, ...., category_id) 
images(id, path, place_id) 

現在,在此基礎上建成了下面的XML

categories.xml

<?xml version="1.0" encoding="utf-8"?> 
<categories> 
    <category> 
     <name>Hill Station</name> 
     <icon>hill-station</icon> 
    </category> 

    <category> 
     <name>Water Fall</name> 
     <icon>water-fall</icon> 
    </category> 
. 
. 
. 
. 

</categories> 

places.xml

<places> 
    <place> 
     <name>Nandi hills</name> 
     <category>hill-station</category> 
     <distance>70</distance> 
     <days>1</days> 
     <latitude>13.3333</latitude> 
     <longitude>77.6500</longitude> 
     <weather>Summer 25°-28°C, Winter 8°-10°C</weather> 
     <todo>Para-Gliding, Tippu Drop, Gardens, Jumping Jacks</todo> 
     <about>Nandi Hills or Nandidurg is an ancient hill fortress of southern India, 
      in the Chikkaballapur district of Karnataka state. It is located just 10 km from 
      Chickballapur town and approximately 60 km from the city of Bangalore. The hills are 
      nestled between the neighboring towns of Nandi, Muddenahalli, and Kanivenarayanapura. 
      The hills are traditionally held as the origin of the Arkavathy river. 
     </about> 
     <images> 
      <image>http://i.imgur.com/Y2MMa.jpg</image> 
      <image>http://i.imgur.com/DoRXC.jpg</image> 
      <image>http://i.imgur.com/ZA8PR.jpg</image> 
      <image>http://i.imgur.com/aSeYa.jpg</image> 
     </images> 
    </place> 

    <place> 
     <name>Kemmannugundi</name> 
     <category>hill-station</category> 
     <distance>260</distance> 
     <days>2</days> 
     <latitude>13.5470</latitude> 
     <longitude>75.7580</longitude> 
     <weather>Summer 17°-34°C, Winter 12°-29°C</weather> 
     <todo>Rajendra hill, Rock garden, Z point, Shiva temple</todo> 
     <about>Kemmannugundi is a hill station in Tarikere taluk of Chikkamagaluru district in the state of Karnataka, India. 
      It is 1434m above sea level. This was the summer retreat of Krishnaraja Wodeyar IV and as a mark of respect to the king, 
      it is also known as Sri Krishnarajendra Hill Station. The station is ringed by the Baba Budan Giri Range, with cascades, 
      mountain streams, and lush vegetation, Kemmangundi’s has ornamental gardens and mountains and valleys views. 
      There is a spectacular sunset view from the Raj Bhavan.</about> 
     <images> 
      <image>http://i.imgur.com/Y2MMa.jpg</image> 
      <image>http://i.imgur.com/DoRXC.jpg</image> 
      <image>http://i.imgur.com/ZA8PR.jpg</image> 
      <image>http://i.imgur.com/aSeYa.jpg</image> 
     </images> 
    </place> 
</places> 

我使用OrderXMLHandler現在來分析數據。有沒有任何lib可以解析xml並自動給對象?

回答

1

看看Castor。我很多年前就使用過它。這似乎仍然是一個活躍的項目。它提供XML/Object marsahlling/unmarshalling:http://castor.codehaus.org/index.html

JAXB提供了相同的功能,但我聽說(即沒有確認)在Android上運行JAXB時出現問題。