How to retrieve external XML files into android application - android

In my Android application, I want to retrieve an XML file from the internet and parse some tags in that file to my application. I got a working tutorial here which uses XMLResourceParser but that tutorial points out how to read an XML file from a local directory.
What are the method calls to retrieve an external XML file into my Android application?
XmlResourceParser xrp = this.getResources().getXml(R.xml.test);

You can get an InputStream to the XML file using the following code:
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)connection;
httpConn.setDoInput(true);
httpConn.setRequestProperty("charset", "utf-8");
int responseCode = httpConn.getResponseCode();
if(responseCode != HttpStatus.SC_OK) {
InputStream xmlStream = httpConn.getInputStream();
// Pass the xmlStream object to a XPP, SAX or DOM parser.
}

create a new folder "raw" in "res", put external xml in raw folder
InputStream inputStream = getResources().openRawResource(R.raw.yourXMLname);

If by "external XML file" you mean loading an XML file from the internet, check out this article on HelloAndroid for how to download an URL to your device.
In terms of the actual XML parsing, I've found SJXP "Simple Java XML Parser" to be the simplest solution. It's lightweight, easy to use, and works using callback handlers. The website also has some sample code which is very helpful.
http://www.thebuzzmedia.com/software/simple-java-xml-parser-sjxp/

Related

uri to bitmap conversion fails [duplicate]

In my application I would like to use a gpx file downloaded from the server.
I use GPXParser from https://github.com/ticofab/android-gpx-parser .
When I was trying to parse gpx file, I got warring "java.io.FileNotFoundException: No content provider"
Gpx parsedGpx = null;
GPXParser parser = new GPXParser();
InputStream inputStream = getContentResolver().openInputStream(Uri.parse(stringUrl));
parsedGpx = parser.parse(inputStream);
How can I solve my problem?
getContentResolver is to get data from a ContentResolver not from any random Url, it is for getting data from other processes and usually takes the string form of content://....
You need to download the gpx first with something like HttpURLConnection and it's getInputStream() method to get an inputStream to use with the parser.

XmlPullParser setInput issue

I have problem with setInput() method. When I've provided plain StringReader and parse XML
locationsXml.setInput(new StringReader("<locations>" + "<location>" etc.[...]
Everything gone well, but if I want to load file from resources:
locationsXml.setInput(new FileReader("res/xml/locations.xml"));
File is not being loaded.
Where is the problem?
You have create a Inputstream from the file in your resources (provided the resource xml is placed in 'assets' folder). You can get control of that resource using :
context.getAssets.open(name of the file);
eg - InputStream myInput = myContext.getAssets().open(locations.xml);
Noww - you can use this inputStream to do anything you want.
Or
Put the xml into /res/raw folder. It looks like openRawResource opens resources from that folder only. You can also try getResources().getXml(com.MYCLass.R.xml.programs); which will return you instance of XML parser.
InputStream object = this.getResources()
.openRawResource(R.raw.fileName);

Specify folder path to XML file in Android

I put my employeedetailxml into a folder named "res\raw". When I attempt to open it by specifying the file and folder name, I get the error "file not found".
How can I specify the path to my file?
I would much prefer to be able to pass 'R.raw.employeedetailxml' to FileInputStream to specify that it open that file. Is that possible? (I tried it, and got an error.)
Can FileInputStream take a Resource ID as parameter?
try{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader xr=sp.getXMLReader();
EmployeeDetailHandler empDetailHandler=new EmployeeDetailHandler();
xr.setContentHandler(empDetailHandler);
xr.parse(new InputSource(new FileInputStream("\\res\\raw\\employeedetailxml.xml")));
}
Yes, you can use something similar to passing R.raw.employeedetailxml. You just need to fetch the resource XML file using that resource name, like so:
InputStream ins = getResources().openRawResource(R.raw.file_name);
Try this:
InputStream inputStream =
getResources().openRawResource(R.raw.employeedetailxml);
this.getResources().openRawResource() returns an InputStream that will probably help you to parse the xml.
If placing your xml file under assets doesn't hurt, you can try out this one:
AssetManager manager = getAssets();
InputStream inputStream = manager.open("pathUnderAssets/filename.xml");
Please note that getAssets() call is made on context. So, you must call it with context in classes which is not an Activity.

How to parse a local xml file using dom in android?

I have seen lots of examples about parsing local xml files with Sax but i need to do this with DOM. My xml file is under res/xml folder. Here is the code:
InputStream in = getResources().openRawResource(R.xml.maps);
StringBuffer str = new StringBuffer();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
DocumentBuilder db = dbf.newDocumentBuilder ();
Document document = db.parse(in, null);
document.getDocumentElement ().normalize ();
But it doesnt work. I think something missing with the line InputStream. It is really important for me, i need your help.
I had the same. I was wondering why this openRawResource gets the XML file corrupted, and that's because it was placed in r.xml.* not in r.raw.* :)
I have solved the problem. Just need to change small thing:
InputStream in = this.getResources().openRawResource(R.raw.bu);
I added "this" to that line and also I have put the maps.xml file under res/raw.

How to read local xml file is resource folder as a input stream in android?

I am trying to get input stream from something like this.
InputSource myInputSource = new InputSource(activity.getResources().openRawResource(com.MYCLass.R.xml.programs));
myXMLReader.parse(myInputSource);
and then call parse on the parser instance i Created. Some how i get nothing.
Works fine if I use a server XML....
Put the xml file into /res/raw folder. It looks like openRawResource opens resources from that folder only. You can also try getResources().getXml(com.MYCLass.R.xml.programs); which will return you an instance of XML parser.
A piece of code taken from #Krupa
InputStream object = this.getResources()
.openRawResource(R.raw.fileName);
You can read the file by the following code :
InputStream object = this.getResources()
.openRawResource(R.rawFolderName.fileName);

Categories

Resources