0

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

shelll
  • 3,234
  • 3
  • 33
  • 67
Burjua
  • 12,506
  • 27
  • 80
  • 111

3 Answers3

3

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.

Community
  • 1
  • 1
LarsH
  • 27,481
  • 8
  • 94
  • 152
  • Thank you very much for answer, I know about XSLT but as I understood it is not supported in android (I could not find anything about it neither in SO nor in Google) . About code you provided, android does not provide the javax.xml.transform package, so I can't use all this classes. I've read that it is not recommended to add external classes to android. Is it true? – Burjua Sep 15 '10 at 14:44
  • Ok, despite recommendations, I imported this package, now I'm getting `Conversion to Dalvik format failed with error 1` – Burjua Sep 15 '10 at 15:35
  • @Burjua: "android does not provide the javax.xml.transform package" What version of the API are you using? The android doc I linked to says "since API level 8". – LarsH Sep 15 '10 at 16:13
  • Actualy I'm using API level 3 as I want android since 1.5 to be able to run my application. Indeed after I changed project build target it works, but what about older versions of android? – Burjua Sep 15 '10 at 16:41
  • @Burjua, http://www.ibm.com/developerworks/opensource/library/x-android/index.html 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. – LarsH Sep 15 '10 at 17:11
  • Ok, thanks for a great answer and comments, I didn't want to parse xml, but apparently I will have to. If you or anyone else have other ideas, please post them)) – Burjua Sep 15 '10 at 17:25
0

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:

  1. 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
  2. The example of its usage: See HelpActivity.java

  3. The XML file to be transformed: changes.xml and the corresponding XSL stylesheet: changesxsl.xsl

yvolk
  • 2,391
  • 3
  • 21
  • 26
0

Use XSLT, XSLT convert your XML in to HTML that we can display in Android Webview. This question will help.

Community
  • 1
  • 1
Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70