views:

92

answers:

1

Hi guys, I'm relatively new to java and android, in my android application I need to get xml file, transform it and show to user.

I know how to parse xml, but I don't want to parse it and generate view after. I'd like to transform it to html and display in 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

+1  A: 

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.

LarsH
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
Ok, despite recommendations, I imported this package, now I'm getting `Conversion to Dalvik format failed with error 1`
Burjua
@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
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
@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
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