XML newbie Q, selecting xml nodes/elements undefined?



I've been given an assignment to create a JavaScript script that is supposed to read through an xml file.


This is the entire .js file



console.log("Script Connected.");
$(document).ready( function () {
console.log("DOM Ready.");
var request = $.ajax(
{
type: "GET",
url: "massEffect.xml",
data: "null",
datatype: "xml"
})
.done(function( data ) {
//function to run on successful completion
var xml = $( data );
var rootNode = xml.find("conversation");
var choice1 = rootNode.children();
// var choices = rootNode.children();
console.log("massEffect.xml Loaded.");
console.log(choice1.tagName)
parseDialogue(choice1);

});

function parseDialogue(choice) {
/*Instructor's comments
/*You will want to write a function that
-takes a single <choice> from the XML as an argument and
-parses it to list out the dialogue in its <voice> elements into the <div> with the ID “voices”,
-parses the description attribute of its <choice> elements into the <div> with the ID “choices”.

See index.html for examples of the DOM fragments to build so our CSS styling will work for you.*/
var voices = choice.tagName;
var after = choice.eq(1);
console.log(choice)
console.log(voices);
//console.log(voices.children())
// console.log(after);
console.log("function called");
}

} );


The long XML File simply just follows this format, which is inside the rootNode <conversation>



<choice>

<voices>

<voice></voice>



<voice></voice>

</voices>

<after>

<choices> <!—Having a <choices> element inside <after> is optional -->

<choice>

<!—This <choice> follows the same structure as the one you’re looking at -->

</choice>

</choices>

</after>

</choice>


right now as you can see, I'm testing out ways to grab data from the XML file and from the nodes/elements themselves. I've been going through the w3school tags for the nodes and elements and everything pretty much returns an undefined value. except for when i use .children() for the nodes. So I was thinking I've been using the wrong methods this whole time.


Questions and Extra Details:


1.) We have been instructed not to use XMLHTTPREQUEST (every site I come across so far uses this type of request so I cannot tell if my code is right) so I've been going with Ajax, but is everything looking like its in its proper order?


2.)My main Q: why are commands like choice.firstChild, choice.tagName... returning undefined? choice.children() seems to be the only thing working.


http://ift.tt/1lYVu0O http://ift.tt/1tVwA13


If I'm using the wrong methods, can someone provide me with the correct link?


No comments:

Post a Comment