I'm new to React-Native. Trying to make some very simple apps. Can't figure out how to fetch XML data. With JSON everything is clear and simple.
But how to fetch XML? Tried to convert it to JSON via this and some other similar scripts, but without any success. Need help :/
My code looks very simple:
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>
);
},
});
Before beginning the React app guide, you must have XML data to show, which you may need to collect from the server. To fetch the data from the server, use the HttpClient Axios, whichis installed using the below npm command. Now that you have installed Axios, create a new component, for example, ParseXml.
XML documents are widely used in Android to create UI layouts, which can be used to provide native interface support via native modules in React Native. XML is also the only supported data transfer format for APIs that are built using SOAP protocol.
The page uses the XMLHttpRequest (JavaScript) object to fetch the XML file (sample. xml) then parses it in JavaScript and creates the chart. The function that parses the XML response and then uses the data to create the chart is shown below and called myXMLProcessor() (it's the XMLHttpRequest callback function).
JSX stands for JavaScript XML. JSX allows us to write HTML in React. JSX makes it easier to write and add HTML in React.
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
looks like you cannot get XML back but you can get the raw text; use response.text()
instead of response.json()
. you will still need to process the text into xml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With