I'm trying to read a xml file from sdcard. I need to parse it to a string array.
XML (a.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="Timetable">
<item>05:40</item>
<item>06:00</item>
<item>06:16</item>
<item>06:28</item>
<item>06:40</item>
<item>07:16</item>
<item>07:29</item>
<item>07:43</item>
<item>07:55</item>
<item>08:07</item>
<item>08:22</item>
<item>08:34</item>
<item>08:46</item>
<item>08:58</item>
<item>09:10</item>
<item>09:22</item>
<item>09:34</item>
<item>09:46</item>
<item>12:10</item>
<item>12:22</item>
<item>12:34</item>
<item>12:46</item>
</string-array>
</resources>
I have tried to use two different functions, and none worked, the final string was empty.
Function 1:
public void function(){
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("FindMyBus/a.xml");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Function2:
public void prueba(){
File file = new File(Environment.getExternalStorageDirectory()
+ "FindMyBus/a.xml");
StringBuilder contents = new StringBuilder();
try {
//use buffering, reading one line at a time
//FileReader always assumes default encoding is OK!
BufferedReader input = new BufferedReader(new FileReader(file));
try {
String line = null; //not declared within while loop
/*
* readLine is a bit quirky :
* it returns the content of a line MINUS the newline.
* it returns null only for the END of the stream.
* it returns an empty String if two newlines appear in a row.
*/
while (( line = input.readLine()) != null){
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
String data= contents.toString();
}
Can anyone help me?
if you need to parse the xml file you need to use a xml parser. get it as seperate strings and then save it into a string array
Please follow this link to understand better
http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
when you get the parsed string u can add it to the array.