Parsing XML like this
<item>
<RelatedPersons>
<RelatedPerson>
<Name>xy</Name>
<Title>asd</Title>
<Address>abc</Address>
</RelatedPerson>
<RelatedPerson>
<Name>xy</Name>
<Title>asd</Title>
<Address>abc</Address>
</RelatedPerson>开发者_StackOverflow中文版
</RelatedPersons>
</item>
I d like to parse this data with a SAXParser. How can i do this? I know the tutorials about SAX, and i can parsing any normal RSS, but i can't parsing this datas only.
Define your Problem: What you can probably do is create a Value Object(POJO) called Person
which has the properties: name
, title
and address
. You aim of parsing this XML would then be to create an ArrayList<Person>
object. Defining a definite data structure helps you build logic around it.
Choose a Parser : You can then use a SAX
Parser or an XML Pull Parser to browse through the tags: see this lin for a tutorial on DOM, SAX and XML Pull Parser in Android.
Data Population Logic: Then while Parsing, whenever you encounter a <RelatedPersons>
tag, instantiate a new Person
object. When you encounter the respective Properties tag, read the value and populate it in this object. When you encounter a closing </RelatedPersons>
dump this Person Object in the ArrayList. Depending on the Parser you use, you will have to use appropriate methods to browse to the child node/nested nodes.(Refer the link for details)
By the time you are done parsing the last item node you will have all the values in your ArrayList.
Note that this is more of a theoretical answer; I hope it helps.
精彩评论