React-Native fetch XML data
you can try 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)
})
I use it to my project.
you can use npm install xmldom, now load DOMParser as below :
var DOMParser = require('xmldom').DOMParser
I use it to my project.
First of all - React Native using JavaScriptCore
, which is just a javascript interpreter, so no Browser Object Model, no document object, no window, etc. That's a reason why you cannot use DOMParser
for example.
Indeed React provides you a XMLHttpRequest wrapper, but it doesn't include responseXML
.
I didn't found any solution for this, so I've just created my own library for that: react-native-xml which allows you to parse xml and make XPath queries for it:
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