我是个新来的土生土长的人.try 制作一些非常简单的应用程序.无法理解如何获取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)
        })

我把它用于我的项目.

React-native相关问答推荐

如何更改分隔底部标签和屏幕内容的线的 colored颜色 ?React Native Tab导航器

你使用 react native 应该学习 react.js 吗?

React Native 中如何使用 Flex 居中组件?

如何以编程方式在 react-native 中截屏

FlatList contentContainerStyle -> justifyContent: 'center' 导致滚动问题

SyntaxError: const 声明中缺少初始化程序

react native图像不适用于特定 URL

React native - 创建单例模式的最佳方式

使用离线包在 iOS 设备上运行 react-native 应用程序

React Native:组件卸载时的动画

react-native run-android 构建错误':app:generateDebugBuildConfig'

如何在 React Native Android 中设置按钮的高度

TypeError : props.navigation.getParam 不是函数. In(props.navigation.getParam('name')

React Native - React Navigation导航转换

在 React-native 上加载图像时出错:Unexpected character

在 React Native 中将props传递给外部样式表?

如何在 react-native 中设置 Alert 元素的样式?

react-native 开始给出 Invalid 正则表达式无效错误

如何在 react-native 中为带有图标的按钮建模

使用 TouchableWithoutFeedback react-native onPress 不起作用