开发者

How can I transform xml to html on android?

开发者 https://www.devze.com 2023-01-16 11:18 出处:网络
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'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 t开发者_如何学Co 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:

  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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号