0

i searched on my problem and found a lot of useful stuff which i already used.

This is the plan: I want to load data from an xml file into a javascript variable. A DOMParser function which reads the xml data pushes it into an array, so i can get random elements from the array later.

To understand what i want to do: I want to load random youtube videos. The links/endings of the videos are stored in the xml file like "d_HvwKtsy_Q". After a video ends it should start the next one right away. I already figured out how to do this in the YT api.. i just need to load the xml file into a variable, so i can edit it in a extern xml file.

This is what i´ve got so far:

// This is my "xml file", but i want to have it in a extern file, so the variable      
stores the extern xml file data like "externXMLfile.xml"

var xml_string ='<answers><answer id="0">o6f9wJ1DWhY</answer><answer    id="1">72Y7M3_NlfI</answer></answers>'


function get_answers_from_xml_string(xml_string) {
// Parse the XML string into a XMLDocument
var doc = window.DOMParser
            ? new DOMParser().parseFromString(xml_string, 'text/xml')    // Standard
            : new ActiveXObject('Microsoft.XMLDOM').loadXML(xml_string); // IE

// Find the answer nodes
var answers_nodes = doc.getElementsByTagName('answer');
var answers = [];

// Loop through them and save their text content into an array
for (var i = 0; i < answers_nodes.length; i++) {
    answers.push(answers_nodes[i].firstChild.data)
}

return answers;
}


//Save the xml elements in an array called answers
var answers = get_answers_from_xml_string(xml_string);

//This is the youtube api stuff, which works fine
    // create youtube player
    var player;
    function onYouTubePlayerAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'd_HvwKtsy_Q',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange,

          },
          playerVars: {

                    'controls': 0,
                    'showinfo': 0,
                    'iv_load_policy': 3,
                },
        });
    }


//this function loads a new random video id after the video stops from the array, which stores the xml elements

function getMessage() {
return answers[Math.floor(Math.random() * answers.length)];
}

    function swapVideo() {     
player.stopVideo();
player.loadVideoById(getMessage());


}
// autoplay video
    function onPlayerReady(event) {
        event.target.playVideo();
    }

// when video ends
    function onPlayerStateChange(event) {        
        if(event.data === 0) {          
           swapVideo();
        }
    }

i hope you understand my problem i think i figured out almost everything but connecting the xml_string variable with the extern xml file

thanks for your help!!

Marc Ster
  • 2,276
  • 6
  • 33
  • 63

2 Answers2

0

Some thing like this could help you

 System.IO.StreamReader file = new System.IO.StreamReader(@"c:\YourXML.xml");
    string test = file.ReadToEnd();

You can refer this too How can I save the this XML to a C# string variable?

Community
  • 1
  • 1
Darshan
  • 535
  • 1
  • 3
  • 16
  • i understand the intention but i guess this i c# code.. so i don´t know if something like that exists in js or even how to write it in js – Marc Ster Apr 06 '13 at 10:51
  • I don't think that the questioner asked for a node solution?! – powtac Apr 06 '13 at 10:51
  • can look at this too – Darshan Apr 06 '13 at 10:53
  • the point is i can save the xml stuff already in my variable "xml_string".. Like var xml_string ='o6f9wJ1DWhY72Y7M3_NlfI' ... i just want to put the xml stuff in a extern file and connect it with the variable xml_string again! – Marc Ster Apr 06 '13 at 10:53
  • @ Darshan, yeah found this too and seems logical but can´t get it to work in my case :( – Marc Ster Apr 06 '13 at 10:59
  • Finally figured it out! Thanks Darshan for the right link. This version worked for me http://stackoverflow.com/a/8680038/1870659 var jqxhr = $.ajax({ type: 'POST', url: "freeakshow.xml", dataType: 'xml', global: false, async:false, success: function(data) { return data; } }).responseText; xml_string = jqxhr; – Marc Ster Apr 06 '13 at 11:18
0

Finally figured it out! Thanks Darshan for the right link.

https://stackoverflow.com/a/8680038/1870659

var jqxhr = $.ajax({
type: 'POST',       
url: "freeakshow.xml",
dataType: 'xml',
global: false,
async:false,
success: function(data) {
    return data;
}
}).responseText;


xml_string = jqxhr;
Community
  • 1
  • 1
Marc Ster
  • 2,276
  • 6
  • 33
  • 63