gpt4 book ai didi

javascript - React-Native 获取 XML 数据

转载 作者:数据小太阳 更新时间:2023-10-29 01:38:38 32 4
gpt4 key购买 nike

我是 React-Native 的新手。尝试制作一些非常简单的应用程序。无法弄清楚如何获取 XML 数据。使用 JSON 一切都变得清晰简单。

但是如何获取 XML?试图通过 this 将其转换为 JSON和其他一些类似的脚本,但没有任何成功。需要帮助:/

我的代码看起来很简单:

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)
})

我将它用于我的项目。

关于javascript - React-Native 获取 XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29805704/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com