React-Native获取XML数据 [英] React-Native fetch XML data
本文介绍了React-Native获取XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是React-Native的新手.尝试制作一些非常简单的应用程序.无法弄清楚如何获取XML数据.使用JSON,一切都变得清晰而简单.
I'm new to React-Native. Trying to make some very simple apps. Can't figure out how to fetch XML data. With JSON everything is clear and simple.
但是如何获取XML?尝试通过此和其他一些类似脚本将其转换为JSON,但没有成功.需要帮助:/
But how to fetch XML? Tried to convert it to JSON via this and some other similar scripts, but without any success. Need help :/
我的代码看起来非常简单:
My code looks very simple:
var xml_url = 'http://api.example.com/public/all.xml';
var ExampleProject = React.createClass({
getInitialState: function() {
return {
data: {results:{}},
};
},
componentDidMount: function() {
this.fetchData();
},
fetchData: function() {
fetch(xml_url)
.then((response) => response.json())
.then((responseData) => {
this.setState({
data: responseData.data,
});
})
.done();
},
render: function() {
return (
<View style={styles.wrapper}>
<View style={styles.container}>
<View style={styles.box}>
<Text style={styles.heading}>Heading</Text>
<Text>Some text</Text>
</View>
</View>
</View>
);
},
});
推荐答案
您可以尝试 https ://github.com/connected-lab/react-native-xml2js ,
const parseString = require('react-native-xml2js').parseString;
fetch('link')
.then(response => response.text())
.then((response) => {
parseString(response, function (err, result) {
console.log(response)
});
}).catch((err) => {
console.log('fetch', err)
})
我将其用于我的项目.
这篇关于React-Native获取XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文