在Android中使用XSLT转换XML文件 [英] Convert XML file using XSLT in Android

查看:95
本文介绍了在Android中使用XSLT转换XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用XSLT在Android中显示它的XML文件转换为HTML格式。

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?

我也试过了codeS在的Andr​​oid:使用XSLT XML转换 但没有奏效。我不知道什么是StringOutputStream在那里。

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.

在此先感谢。

推荐答案

把两者的XML和XSLT文件中的原始文件夹。 生成的HTML文件,将存储在SD卡。 使用下面的code。

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

这篇关于在Android中使用XSLT转换XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆