Jsoup can't read xml return file - android

I'm facing some problems with Jsoup. I'm trying to retrieve an xml file from Open Movie Database using their API in my beta Android app.
Their API documentation says if the return type will be an xml just put an "r=" and the return file type.
I've tested with some requests. Below one of them:
Jsoup.connect(http://www.omdbapi.com/?i=tt1285016&r=xml).get();
Testing on a browser works fine. But on android no. Any exception is thrown. If I don't insert the return file type, it returns a JSON. In this case I receive the data.
To make sure if the problem is with xml file. I've teste with MusicBrainz API. By default it returns XML. For my surprise works fine.
What is the problem? The return type of open movie database of Jsoup?

Jsoup's primary focus is in dealing with HTML, and making sure that the returned document is well formed HTML. So by default it will always treat the input as HTML and will normalise the document. That's why you're getting a DOM like <html><head></head>...<xml>...</html>.
If you know the input you're giving it is actually XML, you can configure Jsoup to parse in XML mode. In that case it will not normalise to a HTML DOM, and it will not enforce any HTML spec rules.
As an example:
String url = "http://www.omdbapi.com/?i=tt1285016&r=xml";
Document doc = Jsoup.connect(url)
.parser(Parser.xmlParser())
.get();
System.out.println(doc);
Compare that output with and without the Parser.xmlParser() configuration:
In XML mode:
<?xml version="1.0" encoding="UTF-8"?>
<root response="True">
<movie title="The Social Network" year="2010" {snip} />
</root>
In HTML mode:
<!--?xml version="1.0" encoding="UTF-8"?-->
<html>
<head></head>
<body>
<root response="True">
<movie title="The Social Network" {snip} />
</root>
</body>
</html>

Found the problem. The values were always there. I don't know why but the return type is an html file with xml tags included. Printing values on Logcat it returns html tags html, head, body and only after this the XML.

Related

Reading an Image URL from XML using XSL to display web page in web browser

I am formating an xml file using xsl. Part of this involves reading an image URL from the XML file and then displaying the image in a web browser. I have tried two different methods of doing this and both methods work when viewed using firefox, Internet Explorer or Edge on a windows machine.
When I try to view this using Safari or Firefox on an iphone using IOS 10 the document displays correctly except for the image not being displayed. I have also tried this on an Android phone with the same result - no image displayed. My question is can this be done on IOS (or Android) phone web browsers?
The source xml file is below (with unrelated tags to image removed)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="application/xml" href="whats_on.xsl"?>
<show_details>
<production>
<show_title>
Hobsons Choice
</show_title>
<show_image>
<image_path>
../images/show_images/hobsons_choice/hobsons_choice.jpg
</image_path>
<image_alt_text>
Hobsons Choice
</image_alt_text>
</show_image>
... More tags here
</production>
... More tags here
</show_details>
The XSL file to format the document with sections removed:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="show_details">
<html>
....
<xsl:element name="img">
<xsl:attribute name="class">
show_images
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="show_image/image_alt_text"/>
</xsl:attribute>
<xsl:attribute name="src">
<xsl:value-of select="show_image/image_path"/>
</xsl:attribute>
</xsl:element>
....More HTML
</html>
</xsl:template>
</xsl:stylesheet>
I have also tried simply inserting the image using the following method:
<img src="{show_image/image_path}"/>
Neither method seems to work on IOS or Android phones.

Symbols show up as � after i parse an html webpage using jsoup and display contents in android listview

I would like to understand this problem i have been having.
Im parsing an html source page and displaying the content i want in a list view in android.
I parse the page using this command.
doc = Jsoup.connect(myURL).get();
Symbols such as é or “ ” show up as �.
I understand they are not being recognized by the encoding mechanism but is it because of jsoup or android?
Android default encoding im using is Utf-8 should it not support that?
If it should not how and what should i change it to?
Thank you for you help.
é in ISO-8859-1 (extend ASCII) is the value 233 but in UTF-8 it is the value 195 folowed by 169.
You need to know in what encoding the caracters are saved in because only the values are saved and then interpreted.
Thank you guys for the help.
making the jsoup call like this :
Document document = Jsoup.parse(new URL(url).openStream(), "ISO-8859-1", url);
was the way to go i then had to find out the real encoding of the webpage in chrome you can find it in 'more tools' and in my case it was
windowns-1252. One line of code solved the problem:
doc = Jsoup.parse(new URL(url).openStream(), "windows-1252", url);

Read content (XML) from Android WebView

when i try to get the html source code from webview i use this example for my solution:
Extracting HTML from a WebView
now i want to parse a page which has no html content. the page displays only a text extracted from xml source.
does anybody have an idea how to get the content (or xml source code) from webview?
best regards
EXAMPLE: XML
< ID >test< /ID > <BR>
< Status >0< /Status >
Is shown as: test0 in webview
I want get the "test0" and put it to string
Option #1: Use the same approach as is shown in the linked-to blog post. Devise some JavaScript that grabs the data out of your Web page and call that JavaScript via loadUrl(), routing the results to some JavaScript interface you injected into the WebView via addJavascriptInterface().
Option #2: Don't use WebView at all, and use HttpURLConnection or HttpClient to fetch the XML source.

Android XML Rich Text Processing

I used the tutorial listed here to parse my XML:
http://android-er.blogspot.com/2010/05/simple-rss-reader-iii-show-details-once.html
The problem I am having is when I try to read in the XML description tag all I get is this:
The "<" symbol is where the description should go. This is the feed I am reading: http://www.calvaryccm.com/rss/devos.ashx
Please help me solve my issue in getting the real text into the description. Thank you!
I just created an android project in eclipse using the code I downloaded from the site you listed above. I only made one modification to the original sources. I changed line 33 in AndroidRssReader.java to read:
URL rssUrl = new URL("http://www.calvaryccm.com/rss/devos.ashx");
The feed loads and parses fine.
The parsing error is the result of changes you made to the original sources.
If the data is html encoded, you can use one of the following methods -- or if it is unencoded, you can surround the content in CDATA tags.
Spanned spannedContent = Html.fromHtml(htmlString);
textView.setText(spannedContent, BufferType.SPANNABLE);
or
WebView webview = (WebView) findViewById(R.id.aWebView);
String htmlString = "<html><body>Some html</body></html>";
webview.loadData(htmlString, "text/html", "utf-8");
I found out that I need to wrap my RSS tags in CDATA Tags as shown here:
Writing input for .NET web services

How to display an html code(which parsed xml) on android application

I am an android developer. I'm calling web service for one functionality and getting following code as output, when the web service is called.
<string xmlns="http://tempuri.org/">
<Response>
<Response_Code>0</Response_Code>
<Response_Description></Response_Description>
<Description></Description>
</Response>
</string>
In between the Description tag i get html code.
I want to display this on android app.
Can anybody tell me, how i can do this?
Thanks
use the following code as taken from http://developer.android.com/reference/android/webkit/WebView.html
and use a webview to display the data as:
String summary =<WHATEVER IS IN YOUR DESCRIPTION TAG> ;
webview.loadData(summary, "text/html", "utf-8");
You could also use the Html class.
someTextView.setText(Html.fromHtml(descriptionText));

Categories

Resources