Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应本机获取XML数据_Javascript_Xml_Json_Reactjs_React Native - Fatal编程技术网

Javascript 反应本机获取XML数据

Javascript 反应本机获取XML数据,javascript,xml,json,reactjs,react-native,Javascript,Xml,Json,Reactjs,React Native,我是个新来的本地人。尝试制作一些非常简单的应用程序。无法理解如何获取XML数据。使用JSON,一切都清晰简单 但是如何获取XML呢?试图通过和其他类似的脚本将其转换为JSON,但没有成功。需要帮助:/ 我的代码看起来非常简单: var xml_url = 'http://api.example.com/public/all.xml'; var ExampleProject = React.createClass({ getInitialState: function() { r

我是个新来的本地人。尝试制作一些非常简单的应用程序。无法理解如何获取XML数据。使用JSON,一切都清晰简单

但是如何获取XML呢?试图通过和其他类似的脚本将其转换为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>
    );
  },
});
var-xml\u-url='0http://api.example.com/public/all.xml';
var ExampleProject=React.createClass({
getInitialState:函数(){
返回{
数据:{结果:{},
};
},
componentDidMount:function(){
这是fetchData();
},
fetchData:function(){
获取(xml_url)
.then((response)=>response.json())
.然后((响应数据)=>{
这是我的国家({
数据:responseData.data,
});
})
.完成();
},
render:function(){
返回(
标题
一些文本
);
},
});

看起来您无法获取XML,但可以获取原始文本;使用
response.text()
而不是
response.json()
。您仍然需要将文本处理为xml

您可以使用npm安装xmldom,现在按如下方式加载DOMParser:

var DOMParser = require('xmldom').DOMParser

我将它用于我的项目。

首先-使用
JavaScriptCore
进行本机反应,它只是一个javascript解释器,因此没有浏览器对象模型、文档对象、窗口等。这就是为什么不能使用
DOMParser
的原因

React确实为您提供了一个包装器,但它不包括
responseXML

我没有找到任何解决方案,所以我只是为此创建了自己的库:它允许您解析xml并对其进行XPath查询:

let xml = require('NativeModules').RNMXml
xml.find('<doc a="V1">V2</doc>', 
         ['/doc/@a', '/doc'], 
          results => results.map(nodes => console.log(nodes[0]))) 
// Output: 
//  V1 
//  V2 
let xml=require('NativeModules').RNMXml
find('V2',
['/doc/@a','/doc'],
results=>results.map(节点=>console.log(节点[0]))
//输出:
//V1
//V2

我也有同样的问题,花了几个小时才发现我可以这样做:

const GOOGLE_FEED_API_URL = "https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=-1&q="
let url = GOOGLE_FEED_API_URL + encodeURIComponent(<yoururl>);

fetch(url).then((res) => res.json());
const GOOGLE\u FEED\u API\u URL=”https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=-1&q=”
让url=GOOGLE_FEED_API_url+encodeURIComponent();
fetch(url).then((res)=>res.json());

希望有帮助

我使用的是react-native,我不太喜欢xmldom。 我编写了自己的react原生XML解析器。 你可以查一下 你可以试试


我将它用于我的项目。

我使用了两个包的组合。在我的用例中,我必须解析SOAP请求的响应:

第一个包是-使用它的原因不是因为我得到了一个错误。(我也在使用expo。)


我实际用于解析的第二个包是-。我不得不使用第一个包,因为我遇到了相同的错误,即React Native不支持节点标准库。我之所以使用它,是因为在出现大型XML响应时,它提供了更结构化的结果。

下面是一个使用xml2js的工作示例:

•responseText是从example.XML获取的.XML数据

•在.then((responseText)={})的大括号内添加xml2json脚本,以将.xml数据解析为Json格式 log(result)将在终端中呈现json格式

旁注:如果删除解析字符串并添加console.log(responseText),则输出将为XML格式

import React,{Component}来自'React';
从“react native”导入{View};
从“xml2js”导入{parseString}
类应用程序扩展组件{
componentDidMount(){
这个。_isMounted=true;
变量url=”https://example.xml"
获取(url)
.然后((response)=>response.text())
.然后((responseText)=>{
parseString(responseText,函数(err,result){
控制台日志(结果);
返回结果;
});
这是我的国家({
数据源:结果
})
})
.catch((错误)=>{
log('获取提要时出错:',错误);
});
}
组件将卸载(){
这个。_isMounted=false;
}
render(){
返回(
)
}
}

导出默认应用程序是的,response.text()工作正常,但文本到xml的过程存在问题。例如,来自answers的所有解决方案都返回null或error:(另外,在使用新的DOMParser()时,我得到了“找不到变量:DOMParser()”;请尝试使用
response.text()
对此有什么答案吗?我也遇到了同样的问题。没有纯JS XML解析器吗?这是怎么回事?它回答了一个完全不同的问题:如何发送URL作为get参数。我认为它正在使用谷歌服务将XML文件转换为JSON…有人尝试在React Native中专门使用xmldom吗?我在m中使用它y演示项目。你可以参考演示它是否同时支持Android和iOS?@LawGimenez今天我刚刚使用xmldom处理RSS提要。我仍处于早期阶段,但它似乎可以与iOS和Android一起工作。非常漂亮的解决方案,但它似乎不支持Android。因此,你仍然会遇到同样的问题。截至2019年2月,这是一个nswer似乎仍然是最相关的。不过,您可能希望切换到ES6导入语法,以跟上当前的约定:从“react-native-xml2js”导入{parseString};新问题,在npm安装react-native-xml2js后,我得到“找不到模块:无法解析“/Users/me/firsttry/src/components”中的“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)
        })
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)
    })