2013-01-06 128 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时出现问题。