tags:

views:

57

answers:

1

Hello all,

I just read some tutorials in order to parse a xml feed from the web and turn them into a Listview:

URL file = new URL("http://..../file.xml"); 
SAXParserFactory fabrique = SAXParserFactory.newInstance();
SAXParser parseur = fabrique.newSAXParser();
XMLReader xr = parseur.getXMLReader(); 
ReglageParseur gestionnaire = new ReglageParseur();
xr.setContentHandler(gestionnaire);
xr.parse(new InputSource(file.openStream()));

Everything is fine and I am able to parse xml.

My second step is to store the xml file from web into a xml file on the phone and only update it when user ask it. ( In fact, this xml file should not change or maybe once every 6 month, so I don't want to download it each time.)

So, what I did is to store the file on the phone and update it on user demand.

And I can read it by doing:

fIn = openFileInput("fichier.xml");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[255];
isr.read(inputBuffer);
String readString = new String(inputBuffer);

So, for now, everything seem fine and I am nearly happy.

The problem is now when I want to parse the new file on the phone:

xr.parse(InputSource);

I need an InputSource as parameter.

So my question is:

How can I turn my file in the phone into a InputSource? I succeed to have a InputStreamReader or a String but would like to convert that into InputSource.

Thank a lot for any precious help

+1  A: 

Well, I don't know what constructors are available on the Android version, but the J2SE InputSource class has a constructor with a Reader parameter. Have you tried that?

Alternatively, why not just construct an InputSource directly from the InputStream? I assume fIn is a FileInputStream? Why not just call:

InputSource input = new InputSource(fIn);

?

Jon Skeet
Thank a lot, I just found InputSource input = new InputSource(new StringReader(readString)); but your answer is really better!
Profete162
Ouch I did really stupid stuff! Thank a lot for opening my eyes! Stupid question :-p
Profete162