Back to Blog

Process to get data from Youtube XML : Part 7

Process To Get Data From Youtube – 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

  1. 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

  1. state = { videos: [], }
  2. componentWillMount() {
  3. const playerid = this.props.playlistid; /* to get playlist id value*/
  4. /* Now make url of youtube video smal feed */
  5. const url = ‘http://www.youtube.com/feeds/videos.xml?playlist_id=’ + playerid;
  6. /* now its time to fetch data from url */
  7. fetch(url)
  8. .then(Data => Data.text())
  9. /* now we will run console.log to check if data coming after fetch */
  10. .then(DataText => console.log(DataText));
  11. }

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

  1. componentWillMount() {
  2. const getXMLData = (listdata) => {
  3. }
  4. ……

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

  1. const getXMLData = (listdata) => {
  2. const parseData = new DOMParser().parseFromString(listdata, ‘text/xml’);
  3. const allVideoTitle = parseData.getElementsByTagName(‘title’);
  4. const allVideoId = parseData.getElementsByTagName(‘yt:videoId’);
  5. const allThumbnails = parseData.getElementsByTagName(‘media:thumbnail’);
  6. /* define a new array */
  7. const finalArray = [];
  8. /* push all items in one array */
  9. for (const i = 0; i < allVideoId.length; i++) {
  10. finalArray.push({
  11. title: allVideoTitle[i].textContent,
  12. id: allVideoId[i].textContent,
  13. thumbnail: allThumbnails[i].getAttribute(‘url’)
  14. });
  15. }
  16. return finalArray;
  17. };

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

  1. .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.

  1. import React, { Component } from ‘react’;
  2. import { View, Text } from ‘react-native’;
  3. import CardWrapper from ‘../common/CardWrapper’;
  4. import CardInner from ‘../common/CardInner’;
  5. const DOMParser = require(‘xmldom’).DOMParser;
  6. class VideoList extends Component {
  7. state = { videos: [], }
  8. componentWillMount() {
  9. const getXMLData = (listdata) => {
  10. const parseData = new DOMParser().parseFromString(listdata, ‘text/xml’);
  11. const allVideoTitle = parseData.getElementsByTagName(‘title’);
  12. const allVideoId = parseData.getElementsByTagName(‘yt:videoId’);
  13. const allThumbnails = parseData.getElementsByTagName(‘media:thumbnail’);
  14. /* define a new array */
  15. const finalArray = [];
  16. /* push all items in one array */
  17. for (const i = 0; i < allVideoId.length; i++) {
  18. finalArray.push({
  19. title: allVideoTitle[i].textContent,
  20. id: allVideoId[i].textContent,
  21. thumbnail: allThumbnails[i].getAttribute(‘url’)
  22. });
  23. }
  24. return finalArray;
  25. };
  26. const playerid = this.props.playlistid; /* to get playlist id value*/
  27. const url = ‘http://www.youtube.com/feeds/videos.xml?playlist_id=’ + playerid;
  28. fetch(url)
  29. .then(Data => Data.text())
  30. .then(DataText => this.setState({ videos: getXMLData(DataText) }));
  31. }
  32. render() {
  33. return (
  34. <View>
  35. <CardWrapper>
  36. <CardInner>
  37. <Text>For Title</Text>
  38. </CardInner>
  39. <CardInner>
  40. <Text>For Image</Text>
  41. </CardInner>
  42. <CardInner>
  43. <Text>For Buttons</Text>
  44. </CardInner>
  45. </CardWrapper>
  46. </View>
  47. );
  48. }
  49. }
  50. export default VideoList;
Back to Blog
top