簡體   English   中英

如何從本地存儲中的文件而不是 res/raw 中的硬編碼文件中進行選擇?

[英]How can I choose from files in local storage instead of from a hardcoded file in res/raw?

我正在嘗試編寫一個應用程序,它將獲取一個 XML 文件並使用 XSLT 將其轉換為 HTML。

XML 文件的結構將始終相同,但其中的數據會發生變化。

我已經設法使用 res/raw 中硬編碼的 XML 文件讓它工作。

如何更改應用程序以便我可以從設備的本地存儲中選擇 XML 文件?

這是我的代碼:

我有一個 MainActivity ,只有一個按鈕可以啟動正在進行轉換的活動:

主要活動

 package com.example.xml_html_webview; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button)findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, LoadXSLTinWebview.class)); } }); } }

活動_main.xml

 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

進行轉換的活動:

 package com.example.xml_html_webview; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.print.PrintAttributes; import android.print.PrintDocumentAdapter; import android.print.PrintJob; import android.print.PrintManager; import android.util.Log; import android.view.Window; import android.webkit.WebView; import com.example.xml_html_webview.R; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import javax.xml.transform.Result; 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; public class LoadXSLTinWebview extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_PROGRESS); WebView webview = new WebView(this); setContentView(webview); //Reading XSLT String strXSLT = GetStyleSheet(R.raw.xsltfile); //Reading XML String strXML = GetStyleSheet(R.raw.xmlfile); /* * Loading XSLT... */ //Transform ... String html=StaticTransform(strXSLT, strXML); // HTML button added to transformed XML String html_button= "<html><script type=\\"text/javascript\\">function createWebPrintJob() {\\nAndroid.createWebPrintJob();\\n}</script><body><input type=\\"button\\" value=\\"Print\\" onClick=\\"createWebPrintJob()\\" />\\n</body></html>"; //Loading the above transformed CSLT in to Webview... webview.loadData(html + html_button,"text/html",null); } /* * Transform XSLT to HTML string */ public static String StaticTransform(String strXsl, String strXml) { String html = ""; try { InputStream ds = null; ds = new ByteArrayInputStream(strXml.getBytes("UTF-8")); Source xmlSource = new StreamSource(ds); InputStream xs = new ByteArrayInputStream(strXsl.getBytes("UTF-8")); Source xsltSource = new StreamSource(xs); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(xsltSource); transformer.transform(xmlSource, result); html = writer.toString(); ds.close(); xs.close(); xmlSource = null; xsltSource = null; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return html; } /* * Read file from res/raw... */ private String GetStyleSheet(int fileId) { String strXsl = null; InputStream raw = getResources().openRawResource(fileId); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int size = 0; // Read the entire resource into a local byte buffer. byte[] buffer = new byte[1024]; try { while ((size = raw.read(buffer, 0, 1024)) >= 0) { outputStream.write(buffer, 0, size); } raw.close(); strXsl = outputStream.toString(); Log.v("Log", "xsl ==> " + strXsl); } catch (IOException e) { e.printStackTrace(); } return strXsl; }

好吧,我設法找到了使用 FileUtils 將我的文件轉換為字符串的解決方案:

 File InstanceXmlFile = Collect.getInstance().getFormController().getInstanceFile(); String strXML = null; try { strXML = FileUtils.readFileToString(InstanceXmlFile, "utf-8"); } catch (IOException e) { e.printStackTrace(); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM