【发布时间】:2015-06-30 14:11:38
【问题描述】:
我是 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>
);
},
});
【问题讨论】:
-
尝试使用
response.text() -
对此有任何答案吗?我遇到了同样的问题。那里没有纯 JS XML 解析器吗?
标签: javascript xml json reactjs react-native