Reputation: 9
i am working on healthcare project and i have to import some medicen data from another server . the other server data is type xml , and i want to convert it to json to be on my API .
fetch ("http://api.com/rest/api/something&q=")
.then(response => response.text())
.then((response) => {
parseString(responseText, function (err, result) {
console.log(response)
});
}).catch((err) => {
console.log('fetch', err)
})
},
i get this erorr :
fetch ReferenceError: parseString is not defined
i am using ReactJs so please can someone help me to get the correct way to convert XML to JSON ?
Upvotes: 0
Views: 3142
Reputation: 31
install these two npm Lib
1.npm install react-native-xml2js
2.npm install --save xml-js
step-1-----
import axios from "axios";
const parseString = require('react-native-xml2js').parseString; //step-1 here
axios.get(your api url here,
{
headers:{Authorization:this.state.token}
}).then(response => {
parseString(response, function (err, result) {
console.log("response----"+response.data)
//step--2 here
var convert = require('xml-js');
var xml = response.data
var result1 = convert.xml2json(xml, {compact: true, spaces: 4});
var result2 = convert.xml2json(xml, {compact: false, spaces: 4});
console.log("result1----"+result1);
console.log("result2----"+result2);
//step--2 end here
});
}).catch((err) => {
console.log('fetch', err)
})
use which result suitable for you... It's worked for me in my React-native project
Upvotes: 1