Have a look at the feed here. It is easy to process XML 1.0 / 1.1 where they only have normal characters in the <description> tag. However, the RSS feed mentioned above has HTML tags like <strong>, <em>, etc and not to mention, JavaScript function calls and special characters.
What I, as a beginner to Android, do is to make my own SAX parser to get the data from specific tags, put them into objects that represent feeds and make an ArrayList / Vector out of them. All I get is character data in the characters(..) callback method of DefaultHandler.
Now, how do I properly display this text with all the HTML formatting, the JavaScript function calls, etc in Android?
I mean, the one who made the RSS feeds for this put it all this in because they wanted the feeds to hav a certain look-and-feel. Please help me with this.
Don't reinvent the wheel..
As recommended here,use android-rss library to read parts of RSS 2.0 feeds.I have used it in my project and it works brilliantly..
RSSReader reader = new RSSReader();
RSSFeed feed = reader.load(feedUrl);
List<RSSItem> list = feed.getItems();
for (RSSItem i: list)
{
i.getTitle();//title content
i.getDescription();//description content
i.getLink();//link
}
To view the description content use Html.fromHtml which would return styled text..
TextView textView= (TextView)findViewById(R.id.textView1);
textView.setText(Html.fromHtml(descriptionContent));
Related
I want to be able to read ID3v2 tags from mp3 files. I have found multiple frameworks which allow me to get the basic tags. But I would like to be able those in this list. I don't care about backwards compatibility with IDv1.
I already had a look at Jaudiotagger and mp3agic. I didn't find out how to use them for custom tags. Is this possible?
You've to use the string identifier "TXXX", that's the main difference between "normal" and custom fields. I think the following code should be self-explanatory:
AudioFile audioFile = AudioFileIO.read(File songFile) // try and catch
MP3File mp3 = (MP3File) audioFile;
AbstractID3v2Tag v2Tag = mp3.getID3v2Tag();
// Since you've mentioned a list
List<TagField> tagList = v2Tag.getFields("TXXX")
That's one way to do it. The List has now all tags with the TXXX identifier. Now you can simply call toString() on every element in the list and you should get something along those lines:
Description="Play Count"; Text="1.000.000"
I'm writing an android magazine reader for a campus publication I work for. We use wordpress to publish our website, and I want to leverage the wordpress REST API to pull stories (posts) directly from the website, without publishers having to take any additional steps to publish posts on the app after publishing them on the site. I'll do this by getting JSON objects representing posts and deserializing them into POJOs of the Story class (defined in the android application), around which views will then be built dynamically.
I've just discovered the Wordpress REST API and am really excited because I think that the implementation as described above is going to be pretty simple. Are there any obvious roadblocks that I'm missing that might complicate things?
I know that the API responds with a "content" parameter that is a string containing the HTML code for the post, with references to included images/media in the appropriate places. How can I get Android to load that html and display it properly in a WebViewer?
If you don't want to parse the html and separately load images and other resources, simply use
loadDataWithBaseURL like so:
WebView storyView = (WebView) findViewById( .... );
String htmlToDisplay = ....;
storyView.loadDataWithBaseURL( "http://storysite.com/', htmlToDisplay, mimeType, encoding, "" );
The baseURL will be prepended to all relative partial URIs found in the document, so that the WebView can take care of loading all other assets for you.
What im trying to do is parse xml through java. and i only want a snippet of text from each tag for example.
xml example
<data>\nSome Text :\n\MY Spectre around me night and day. Some More: Like a wild beast
guards my way.</data>
<data>\nSome Text :\n\Cruelty has a human heart. Some More: And Jealousy a human face
</data>
so far i have this
NodeList ageList = firstItemElement.getElementsByTagName("data");
Element ageElement =(Element)ageList.item(0);
NodeList textAgeList = ageElement.getChildNodes();
out.write("Data : " + ((Node)textAgeList.item(0)).getNodeValue().trim());
im trying to just get the "Some More:....." part i dont want the whole tag
also im trying to get rid of all the \n
If you're not restricted to the standard DOM API, you could try to use jOOX, which wraps standard DOM. Your example would then translate to:
// Use jOOX's jquery-like API to find elements and their text content
for (String string : $(firstItemElement).find("data").texts()) {
// Use standard String methods to replace content
System.out.println(string.replace("\\n", ""));
}
I would take all of the element text and use regular expressions to capture the relevant parts.
I am trying to parse the images that are display in this link http://lawncare.ncsu.edu/RSSFeed.aspx and display it in an android device. Right now, I am only able to parse the text associated with the images. Can anyone suggest any ideas on how to go about parsing these images? Preferably not using JSoup because I am already half way down the code.
Try looking at this question over here at : How to parse XML using the SAX parser
specifically the answer given by damiean and his solution.
This is a part of my code where i am parsing the rss feed :
Bundle b = new Bundle();
b.putString("title", feed.getItem(position).getTitle());
b.putString("description", feed.getItem(position).getDescription());
b.putString("content", feed.getItem(position).getContent());
b.putString("link", feed.getItem(position).getLink());
b.putString("pubdate", feed.getItem(position).getPubDate().toString());
What i have done is that the "field" contains both the description(text) and the image . I passed the whole "content" field contents into a WebView and it displays the image as well as the text correctly.
I'm relatively new to java and android, in my android application I need to get an XML file, transform it and show it to a user.
I know how to parse XML, but I don't want to parse it and generate views after. I'd like to transform it to an HTML and display in a WebView.
I'm trying to find something on the Internet but can't find anything.
How can I do it? Any ideas or links will be appreciated,
thanks
The usual tool to use for transforming XML to HTML is XSLT. Search SO for XSLT tutorial and you'll get some good results.
Here is another question showing how one developer used XSLT on Android.
You can also search for examples of the use of Transformer in Java, as in this helpful article:
// JAXP reads data using the Source interface
Source xmlSource = new StreamSource(xmlFile);
Source xsltSource = new StreamSource(xsltFile);
// the factory pattern supports different XSLT processors
TransformerFactory transFact =
TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, new StreamResult(System.out));
Update:
For older versions of the Android java API:
This article shows how to use a SAX parser to parse the input XML, then use XmlSerializer to output XML. The latter could easily output whatever XHTML you want. Both are available since API level 1.
Unfortunately I don't see a way to do XPath in API level 3, but if your input XML isn't too complex you should be able to code your own transformations. I know you "don't want to parse it and generate view after", but if you mean you don't want to even use an XML parser that's provided by Android, then I don't know of any alternative.
Update 2:
I just learned about XOM, which supports a subset of XPath. This question shows someone using XOM on Android (to write XML) with API level 4. You could take advantage of the XPath features, as well as the serialization features. It requires a small external library, XOM's jar. I don't know if it's compatible with API level 3.
Please see the complete working example, created as a part of "AndStatus" application.
It uses XSLT to show localized (!) Application Change Log in a WebView of the HelpActivity.
The working example consists of these parts:
The small utility class without any dependencies (i.e. it may be easily reused) which has this function:
/**
Transform XML input files using supplied XSL stylesheet and show it in the WebView
#param activity Activity hosting the WebView
#param resView WebView in which the output should be shown
#param resXml XML file to transform. This file is localized! It should be put into "raw-" folder
#param resXsl XSL stylesheet. In the "raw" folder. May be single for all languages...
*/
public static void toWebView(Activity activity, int resView, int resXml, int resXsl) {
...
}
See full source code here: Xslt.java
The example of its usage: See HelpActivity.java
The XML file to be transformed: changes.xml
and the corresponding XSL stylesheet: changesxsl.xsl
Use XSLT, XSLT convert your XML in to HTML that we can display in Android Webview.
This question will help.