0

I'm new to react native and trying to parse through XML. I'm making a rest call which gives me the response in XML. This XML has a list of objects, which I want to access. I'm getting undefined or empty objects when i'm doing it.

I checked few links but it didn't help: iterate over JSON in react native React-Native fetch XML data React Native: Iterate through nested objects to display values How to iterate through JSON object in react native Code:

<View key={dta.diagnosis_name} style={styles.outerContainer }>
      <TouchableOpacity activeOpacity={0.3} onPress={async() => {
        const response = await fetch('https://something', { method: 'GET', }); 
        var res = await response;
         { res.map((item) => (
             console.log({item.feed.id});
             console.log({item.feed.title});
             console.log({item.feed.summary});
          ))}                           
          }}>
    </TouchableOpacity>
</View>

XML:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:v3="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <title type="text" xml:lang="en-US">Main Title</title>
    <subtitle type="text" xml:lang="en-US">This is subtitle</subtitle>
    <id>tag: sometag</id>
    <updated>2020-09-20T22:43:26Z</updated>
    <category term="c1" scheme="c1scheme" />
    <category term="c2" scheme="c2scheme" />
    <link hreflang="en-US" rel="alternate" href="https://www.yahoo.com" />
    <entry xml:lang="en-US">
        <id>id1</id>
        <title type="text" xml:lang="en-US">Title one/title>
        <summary type="html" xml:lang="en-US">
            &lt;div&gt;Test data 1&lt;div&gt;
        </summary>
        <updated>2020-09-20T22:43:26Z</updated>
        <link hreflang="en-US" type="html" rel="alternate" href="https://www.google.com" />
    </entry>
    <entry xml:lang="en-US">
        <id>id2</id>
        <title type="text" xml:lang="en-US">Title twi</title>
        <summary type="html" xml:lang="en-US">
            &lt;div&gt;Test data 2&lt;div&gt;
        </summary>
        <updated>2020-09-20T22:43:26Z</updated>
        <link hreflang="en-US" type="html" rel="alternate" href="https://www.bing.com" />
    </entry>
</feed>
TheDeveloper
  • 1,127
  • 1
  • 18
  • 55
  • why are you awaiting `response`? or is it `response.json()`? – diedu Sep 21 '20 at 03:15
  • your code is quite broken, a lot of syntax errors, probably in the process of extracting it you missed some things, can you provide a correct version please – diedu Sep 21 '20 at 03:35

2 Answers2

1

According to this answer your code would look like this using react-native-xml2js

import { parseString } from "react-native-xml2js";

...

<View key={dta.diagnosis_name} style={styles.outerContainer}>
  <TouchableOpacity
    activeOpacity={0.3}
    onPress={async () => {
      const response = await fetch("https://something", { method: "GET" });
      const text = await response.text();
      const parsedData = await parseString(text);
      // not sure if parseData would actually be an array, you can console.log to check
      const items = parseData.map((item) => {
        console.log(item.feed.id);
        console.log(item.feed.title);
        console.log(item.feed.summary);
        return item;
      });
    }}
  ></TouchableOpacity>
</View>;
diedu
  • 19,277
  • 4
  • 32
  • 49
  • with this code i am only getting feed.id but not for the list of title and summary inside the `entry` tag. – TheDeveloper Sep 21 '20 at 14:02
  • did you check the structure of the `parsedData` object? could you add the json of that to the question? – diedu Sep 21 '20 at 14:49
0

Answer by @diedu worked.

My updated code is

async func1() {

        var response = await fetch('https://www.Resrtcall.com', {
                                        method: 'GET', });
        var s = await response.text();
        var parseString = require('react-native-xml2js').parseString;
        var xml = s
        var okdss ='';
        parseString(xml, function (err, resssult) { okdss = resssult; });
        var hohoho = Object.keys(okdss).map(key => ({[key]:okdss[key]}));
        var bhb = '';
        var items = hohoho.map((item) => {
            console.log('feed id:' + item.feed.id);
            var abc1 = item.feed.entry.map((abc2) => {
                console.log('entry id:' + abc2.id);
                var abc3 = abc2.title.map((abc4) => {
                console.log('feed.entry title:' + abc4._);
            });
            var abc5 = abc2.summary.map((abc6) => { 
                console.log('feed.entry summary:' + abc6._);
            });
            });
        });
      }
TheDeveloper
  • 1,127
  • 1
  • 18
  • 55