How to parse XML from assets, keep it and add to ListVIew?



I was to told to parse XML file. The task is to parse XML from assets folder and somehow put it into ListView. Moreover, xml contains links and when you click on particular ListView item, you should get file from corresponding link (you should get parsed CSV file, which contains in link. And this parsed CSV must be stored in local database, but that's whole another story)


Here's my XML:



<Table>
<TableItem>
<id>1</id>
<title>bla-bla-bla</title>
<url>http://thelink</url>
<type>CSV</type>
</TableItem>
<TableItem>
<id>2</id>
<title>bla-bla-bla x2</title>
<url>thelink</url>
<type>CSV</type>
</TableItem>
<TableItem>
<id>3</id>
<title>bla-bla-bla</title>
<url>the link</url>
<type>CSV</type>
</TableItem>

.....
</Table>


How do I parse such XML? I've tried XMLparser already and it didn't work for me. And now i've trying SAXParser and I need to describe Handler propperly.



public void parseThat() throws IOException, ParserConfigurationException, SAXException {
InputSource inputSource = new InputSource(this.getActivity().getAssets().open("Table.xml"));
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.parse(inputSource);
}


And here's the Handler:



public class XmlContentHandler extends DefaultHandler
{
List<TableItem> items = new ArrayList<TableItem>();
private boolean inTableItem = false;
private boolean inTitle = false;
private boolean inUrl = false;
private boolean inType = false;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if(localName.equals("TableItem"))
{

}
}

class TableItem{
String title;
String type;
String url;

private TableItem(String title, String type, String url)
{
this.title = title;
this.url = url;
this.type = type;
}
}
}


Any help and ideas would be much appreciated!


No comments:

Post a Comment