Process to get data from Youtube XML : Part 7

If you will see youtube video.xml url, there are many xml node. We  need only all video id , thumbnail url to display image and Title to show on top of Card

XML display data different way than Json. So first we parse xml data and use text to get its value.

 

First install xmldom so run command

 

npm install -S xmldom

 

So after last import line, after a line line break  add parse function which will help to parse data of xml

const DOMParser = require('xmldom').DOMParser;

before component start. For example

…..

const DOMParser = require(‘xmldom’).DOMParser;

class VideoList extends Component {

………

First you need to understand few things before we start coding. In this process I am going to use ComponentWillMount, fetch, and state

componentWillMount: this is a lifecycle method . It invoke immediately before mounting occurs.

Fetch: Fetch Networking api used in react native to get content from arbitrary URL

For more details please take a look here

https://facebook.github.io/react-native/docs/network.html


Now in our next process we will use componentWillMount before render function and so

So in VideoList.js remove condole.log(this.props.playlistid); and start writing componentWillMount()

 

componentWillMount should be called before render() function of component

 

But before componentWillMount function we will define a state video as array. Because after  fetch were are going to get data in array so open VideoList.js and add before render() function

state = { videos: [], }
  componentWillMount() {
  const playerid = this.props.playlistid; /* to get playlist id value*/

  /* Now make url of youtube video smal feed */
  const url = 'http://www.youtube.com/feeds/videos.xml?playlist_id=' + playerid;

  /* now its time to fetch data from url */

  fetch(url)
  .then(Data => Data.text())
  /* now we will run console.log to check if data coming after fetch */
  .then(DataText => console.log(DataText));
  }

Now refresh simulator and you will see xml data in condole.

But this data is not in array and it is not useful for us to setState with array. As you can see we are getting whole xml as it is. To process our video listing we need a array of Title, Image and Video id. To process this we will process to parse data using a helper function . so create a function called getXMLData

 

So in componentWillMount function add a new function

componentWillMount() {
  const getXMLData = (listdata) => {

}


……

No we will expand our function getXMLData. In this function i have passed a parameter which is used to get data which we get from fetch and parse it

So

const getXMLData = (listdata) => {
       const parseData = new DOMParser().parseFromString(listdata, 'text/xml');
       const allVideoTitle = parseData.getElementsByTagName('title');
       const allVideoId = parseData.getElementsByTagName('yt:videoId');
       const allThumbnails = parseData.getElementsByTagName('media:thumbnail');
  /* define a new array */

       const finalArray = [];

/* push all items in one array */

       for (const i = 0; i < allVideoId.length; i++) {
      finalArray.push({
          title: allVideoTitle[i].textContent,
          id: allVideoId[i].textContent,
          thumbnail: allThumbnails[i].getAttribute('url')
        });
      }
      return finalArray;
    };

In above function you can see we have passed our listdata paramer in parseFromString and then we are getting title, video id and Thumbnail by tag name of xml. But is is not enough because we need a series of array in which we can get all values in one array

 

So I created a variable with name finallArray as array then using for function I have adding each values in finalArray using push function

 

Push: This  method help to add new item in  array

 

Now our function returning array of all items which we need in one array. Now we will call this function to setState of Video array state. It may be little confusing but no need to worry much about it . I will show you further process

 

Now go to this line of componentWillMount function

 

.then(DataText => console.log(DataText));

 

And now remove console.log(DataText) and replace with

 

.then(DataText => this.setState({ videos: getXMLData(DataText) }));

Here we have set state video with array using our helper function getXMLData passing Data of fetch

 

Now if you need to check what is result of this video state you can write a line of console.log in render function of component.

 

So go to render function of component and write like this

 

render() {

   console.log(this.state);

   return (

………

 

If you will open console you will see result as video array

 

Now remove console.log from render function.

 

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import CardWrapper from '../common/CardWrapper';
import CardInner from '../common/CardInner';

const DOMParser = require('xmldom').DOMParser;

class VideoList extends Component {
  state = { videos: [], }
  componentWillMount() {
    const getXMLData = (listdata) => {
       const parseData = new DOMParser().parseFromString(listdata, 'text/xml');
       const allVideoTitle = parseData.getElementsByTagName('title');
       const allVideoId = parseData.getElementsByTagName('yt:videoId');
       const allThumbnails = parseData.getElementsByTagName('media:thumbnail');
        /* define a new array */
       const finalArray = [];
       /* push all items in one array */
       for (const i = 0; i < allVideoId.length; i++) {
      finalArray.push({
          title: allVideoTitle[i].textContent,
          id: allVideoId[i].textContent,
          thumbnail: allThumbnails[i].getAttribute('url')
        });
      }
      return finalArray;
    };

    const playerid = this.props.playlistid; /* to get playlist id value*/
    const url = 'http://www.youtube.com/feeds/videos.xml?playlist_id=' + playerid;
    fetch(url)
    .then(Data => Data.text())
    .then(DataText => this.setState({ videos: getXMLData(DataText) }));
  }
  render() {
        return (
      <View>
        <CardWrapper>
          <CardInner>
            <Text>For Title</Text>
          </CardInner>
          <CardInner>
          <Text>For Image</Text>
          </CardInner>
          <CardInner>
          <Text>For Buttons</Text>
          </CardInner>
        </CardWrapper>
      </View>
    );
  }
}
export default VideoList;

 

© Copyrights 2014 by STW Services LLP