1

I am new to Android.

I want to convert a XML file to HTML format using XSLT to display it in android.

Can you please tell me some tutorials or examples to follow?

I also tried the codes in the Android: Convert xml using xslt But it didn't work. I am not sure what is "StringOutputStream" there.

Thanks in advance.

2 Answers 2

4

Put both your xml and xslt file in raw folder. The generated html file will be stored in sdcard. Use the following code.

import java.io.File;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;

public class XsltTester extends Activity {

    private static String TAG = XsltTester.class.getSimpleName();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {

            Source xmlSource = new StreamSource(this.getResources().openRawResource(R.raw.weather));
            Source xsltSource = new StreamSource(this.getResources().openRawResource(R.raw.weatherxsl));

            TransformerFactory transFact = TransformerFactory.newInstance();
            Transformer trans = transFact.newTransformer(xsltSource);
//          FileOutputStream fo = new FileOutputStream(f);
//          fo.write(resizeBitMapImageToByteArray(photoAlbumBean));
//          fo.close();
            File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/mydata.html");

//            OutputStream output = new StringOutputStream();
            StreamResult result = new StreamResult(f);
            trans.transform(xmlSource, result);

        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Thanks Deepak

Sign up to request clarification or add additional context in comments.

5 Comments

@Deepak : Thanks for the reply. This gives me the error "The application has stopped unexpectedly. Please try again". I used the same xsl and xml files used in the given link. Thanks
Download sample xml and xslt file from the url. but give separate name and paste it in raw folder codingwithpassion.blogspot.com/2010/12/…
@Deepak : I used the exact code u gave and put the xml and xslt file in the raw folder.(I created the raw folder manually) Now I don't get any errors, but no output as well. What can I do?
your output is stored in a file you have to pull that file mydata.html in sd card. to see the output oull that file from the sdcard of your emulator
@Deepak : I'm having a little problem in finding the file, mydata.html. How can I find it?
0

Don't forget to add:

< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

to the AndroidManifest.xml as this noob eventually found out :)

1 Comment

This should be included as a comment on the question. While helpful, it doesn't answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.