【问题标题】:Fetch request from xml api and parse repsonse从 xml api 获取请求并解析响应
【发布时间】:2017-11-29 09:49:12
【问题描述】:

我是原生反应的新手,尤其是获取请求的新手。我获取一个使用 xml 文件响应的 Web 服务。我试图解析它并将我想要的值传递给选择器,但是它不起作用!对请求的响应是好的,但问题在于解析它。我尝试了 xmldom 库和 xml12js,没有任何效果...

这是我的代码:

parseSurface(s){
    const DOMParser = require('xmldom').DOMParser;
    console.log('Parsing the feed...');
    let doc = new DOMParser().parseFromString(s, 'text/xml');
    let objs = [];
    let surfs = s.getElementsByTagName('Surfaces');
    //let surfaces = doc.getElementsByTagName('surface:LIBELLE');
     //let surfaces = s.querySelectorAll('Surfaces');
     // let surface = s.querySelectorAll('surface');
     // let libelle = s.querySelector('LIBELLE');
    console.log(surfs)
}

getSurfaces(){
    fetch('myWebService?')
        .then((response) => response.text())
        .then((responseText) => {
            this.parseSurface(responseText);
        })
        .catch((err) => {
            console.log('Error fetching the feed: ', err)
    })

}

componentDidMount(){
    this.getSurfaces();
}

parseSruface 中,如果我使用doc 变量或s 在变量中输入querySelectorAll 时出现此错误,与getElementByTagNamegetElementByName 相同:

获取提要时出错:[TypeError: doc.querySelectorAll 不是函数。 (在 'doc.querySelectorAll('Surfaces')' 中,'doc.querySelectorAll' 未定义)]

我希望我很清楚,我完全迷失了,我会很感激任何帮助,在此先感谢。

【问题讨论】:

  • document 对象是网页的表示。自然,React Native 没有 document 对象。

标签: xml reactjs react-native xml-parsing fetch


【解决方案1】:

请试试这个:

const parseString = require('react-native-xml2js').parseString;



getSurfaces(){
    fetch('http://clubonlinetest.alithya.fr/WebServices/AppliMobile.asmx/getSurfaceList?')
        .then((response) => response.text())
        .then((responseText) => {
            parseString(responseText, function (err, result) {
                console.log(response)
            });
        })
        .catch((err) => {
            console.log('Error fetching the feed: ', err)
    })

}

componentDidMount(){
    this.getSurfaces();
}

【讨论】:

  • 感谢您的回复。我用console.log(result) 更改了responseText,并将xml 转换为Json。现在我认为到达是为了找到我在这个回复中寻找的价值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 2016-05-01
  • 2019-05-13
  • 1970-01-01
  • 2012-03-09
相关资源
最近更新 更多