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)
Related
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
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.
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
I want to strip out the tags from this page http://ptwc.weather.gov/feeds/ptwc_rss_indian.xml
Just I want to take the content between description tags in the above xml
a)For this whether I can use xml parser or regex
b) If I use regex how to strip out that? (I used the below code but it's not working
Pattern p= Pattern.compile("<description>(.*)</description>",Pattern.DOTALL);
//Matcher matcher=p.matcher(result);
Matcher matcher = p.matcher(result);
if (matcher.matches())
{
String s1=matcher.group(1);
textView.setText(s1);
}
else
{
textView.setText("No Matches);
}
You should always use an XML parser. It is much safer and in the end easier to implement. An expression that might work in your tests may break with a nested tag or a minor change in the structure. Also, XML is much more maintainable should you want to retrieve more data from the same files.
Use an XML Parser!
Edit: read an interesting discussion
I agree with Aleadam in that you should always use an XML Parser but I also believe that an XML parser should not be a hassle to write or use which is why I always recommend using the Simple XML Framework on Android.
So much so that I even wrote a blog post on how to use Simple in Android.
I need to get data from an XML file in Android. On the iPhone environment, my code is:
NSURL *thisURL = [[NSURL alloc] initWithString:#"http://www.xxx.com/file.xml"];
NSArray *myArray = [[NSArray alloc] initWithContentsOfURL:providerURL];
myArray is now an array of dictionary items initialized with contents from file.xml.
Is there any way to do this in Android? Can someone point me to doc or sample code?
I'm new to the Android environment and just need some direction.
Thanks,
Kevin
See Working with XML in Android for a variety of methods for dealing with XML. Which method to use depends on how big your XML is, and what you want to do with it. '
I'm not sure how it makes any sense to turn XML into an array, so no, none of the methods do that. If you want something similar to that, use Json instead of XML.
After a bit of research, it appears to me that using the Simple XML Serialization framework is going to be my best bet, especially since I do have a relatively simple XML file to read. The result will be a 'list' class with several 'entry' classes which seems like a viable way to handle this...probably better than having an array of classes as was done in the iPhone app.