首页 > 解决方案 > React Native:如何读取和显示 xml 文件

问题描述

如何将 .xml 文件显示到屏幕上。我可以使用显示 pdf 文件,react-native-pdf但如何在 .XML 文件中显示文本或阅读它。

标签: react-native

解决方案


解决方案在 .XML 文件中显示文本或阅读

我正在向您展示如何读取任何文件。如果您可以读取任何文件,并将其存储到某种状态,那么渲染就不是问题,我希望如此。

以下是如何在 react-native 中读取本地文件:

var RNFS = require('react-native-fs');
import DocumentPicker from 'react-native-document-picker';

selectFiles = () => {
    let that = this;
    try {
        DocumentPicker.pickMultiple({
            type: [DocumentPicker.types.allFiles],
        }).then((results) => {
            console.log(results[0]);
            //that.setState({language: results[0].type});
            RNFS.readFile(results[0].uri)
                .then((file) => {
                    that.setState({
                        code: file
                    });
                })
                .catch((error) => console.log('err: ' + error));
                //the `code` state holds your xml file, just display it however you want... use 3rd party library for syntax highlight or whatever you want
        });
    } catch (err) {
        if (DocumentPicker.isCancel(err)) {
            // User cancelled the picker, exit any dialogs or menus and move on
        } else {
            throw err;
        }
    }
};

推荐阅读