Read XML from R.xml and not as an XMLPullParser - android

I want to pass my xmls from my R.xml directory to Simple library for deserialization.
The library accepts Readers and InputSreams and such.
But from R.xml I get XMLPullParser. How can I get something like a Reader or InputStream instead?

if you save your xml fine inside res/raw instead of res/xml, you should be able to use openRawResource
getResources().openRawResource(R.raw.xml_name)
which returns an InputStream

Related

DOM xml parsing from file res/xml in Android

I looking for solution to parse xml file from res/xml folder with DOM (to read, modify xml).
The method
getResources().getXml(id)
just return XmlResourceParser object, how can I convert it into Document to parse?
I also followed this question but it does not help.
How can I solve that? Thanks in advance!
You should place the xml file in the res/raw folder, and then you can use getResources().getRawResource(R.raw.resource_id) to get an InputStream which you can process using all the standard DOM methods (wrap it in an InputSource, etc).
you can refer this link which is below
https://github.com/commonsguy/cw-android/tree/master/Resources/XML
this is the simple demo of dom parcer which is call xml file from locally.so you can try
also from webservices..try and make sure you get your data

How to Pull an RSS Feed using XmlPullParser in Android

I'm building an RSS reader APP, and I've been told to use the XMLPullParser interface.
Here is the block of code I'm working with:
XmlResourceParser parser = context.getResources().getXml(resource);
'Resource' is a an integer with the R.id. integer of the Xml file. This is not an internal XML file, so I don't know how to work around this.
Any ideas? Is the XmlResourceParser the wrong approach for this project? I've seen XMLReaders used with content handlers as well. Can you integrate these technologies together?
Thank you
what is the type of your xml source?
xmlPullParser can be used to parse any xml sources.
It is in my opinion the way to do this. Only problem you may encounter is when the rss feed has empty lines. The xml pullparser of android (api-level 14) jumps to the /channel if this is the case. When implementing the parser try to use the AsyncTask to start the reading of the rss feed.
Success with the implementation.

Where do I put (and how do I read) my own custom XML in Android?

Sorry, this seems like such an obvious question, but I just can't find an answer in the docs...
I have my own XML doc (not Android related) that I need to pull and parse at run time.
Where do I store it and what's the best way to retrieve it? Do I just read it as a text document into an XML parser? Or does Android come prepared to hand me back a parsed XML object from a well formed xml?
TIA
Put it in a xml folder in res.
You can put it in assets folder of your project.
For parsing you can use openXmlResourceParser(filename) method of AssetsManager class
XmlResourceParser parser = getResources().getAssets().openXmlResourceParser (String fileName);
Best way is to put your xml in assets folder and read xml from assets.
Read xml from assets

How to encode XML on Android?

I need to encode an XML document into a format that will pass through an XML parser as a string (i.e. strip tags). I then need to decode it back again and I need to do this on Android. What is the library/class in the Android API that I'm looking for?
Thanks
XmlSerializer is probably what you want. Use it to build the "outer" XML document; you can give its text(...) method an intact XML string and it will do the escaping for you. You can do the same kind of thing with the DOM API by using setTextContent if you really want to build a DOM tree and then dump that to XML. As you appear to be aware, any XML parser will properly decode the entity references when you ask for the text back.
If you really want to write your own XML-building code, you might try pulling in the Apache commons-lang library into your project for it's StringEscapeUtils class. I'm not aware of anything that will just do the entity substitution without building real XML in the base Android API.
For an example, try this:
XmlSerializer xs = Xml.newSerializer();
StringWriter sw = new StringWriter();
xs.setOutput(sw);
xs.startDocument(null, null);
xs.startTag(null, "foo");
xs.text("<bar>this is some complete XML document you had around before</bar>");
xs.endTag(null, "foo");
xs.endDocument();
Log.v("XMLTest", "Generated XML = " + sw.toString());
I ended up using URLEncoder/URLDecoder, which seems to work nicely.
String encoded = URLEncoder.encode(xml);
The general Java XML Parser is the way to go.
and if you have to build it up manually you can use the XmlSerializer
edit:
Here is an article about working with xml on android, but It uses the XmlSerializer for the writing also
this question is one of the first results, when I searched the solution to this problem, so I answer here
Html.fromHtml(String)

Load and modify xml file in Android

I have an xml file which will be in either assets or res folder which i need to modify by adding some values and then i need to convert the xml into a string to be sent as part of a soap request. What would be the easiest way to achieve this?
You can't modify resource files.
If you don't need modify resources, you can put the XML file in assets folder. Then you can access it by AssetManager (returns by getResources().getAssets()). With AssetManager you can get InputStream from XML file or use XMLResourceParser. And do with it whatever you want.

Categories

Resources