d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help Calling Xml From A Js File. > Paying Fg
Add Reply New Topic New Poll
Member
Posts: 277
Joined: Sep 27 2013
Gold: 0.00
Mar 28 2015 09:47am
The assignment states that I need to create an Ajax application that uses XML and I am allowed to convert an earlier assignment.

The lab we went through in class involved him using an XML document when something is is clicked on and more shows up on the page. I have earlier assignments and I know what I want done but I have no clue how to go about it at all. I have asked for help on several occasions but the teacher is far from helpful and actually meeting with him is nearly impossible as I work full time.

I don't want the entire assignment completed I just want a clear example on one part so I have something to see, understand and work off of. Let me know if you're interested as well as your price and how we would communicate.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 277
Joined: Sep 27 2013
Gold: 0.00
Mar 28 2015 09:59am
Quote (carteblanche @ Mar 28 2015 10:50am)


My file looks nothing like that.

Code
<?php

$details = array (
'firstname' =>
"<?xml version=\"1.0\"?>
<item id=\"firstname\">
<required>Please enter your first name.</required>
<letters>Only letters are allowed in a first name.</letters>
<err>0</err>
</item>");

header("Content-Type: text/xml");
echo $details[$_REQUEST['ImageID']];

?>


This post was edited by CondescendenceX on Mar 28 2015 10:04am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 28 2015 10:05am
Quote (CondescendenceX @ Mar 28 2015 11:59am)
My file looks nothing like that.


that doesnt change the answer. that's how you read an xml document. if you have different tag names, then just parse it differently. getting the file didn't change.
Member
Posts: 277
Joined: Sep 27 2013
Gold: 0.00
Mar 28 2015 10:10am
Quote (carteblanche @ Mar 28 2015 11:05am)
that doesnt change the answer. that's how you read an xml document. if you have different tag names, then just parse it differently. getting the file didn't change.



Sorry if I seem ignorant but we just jumped into doing this and I am completely lost. This is the code we used in the lab, this is what I have to go off of.
Code
window.onload = initPage;

function initPage() {
var thumbs = document.getElementById("thumbnailPane").getElementsByTagName("img");

for (var i = 0; i < thumbs.length; i++) {
var image = thumbs[i];

image.onclick = function() {
var detailURL = "images/" + this.title + "-detail.jpg";
document.getElementById("itemDetail").src = detailURL;
getDetails(this.title);
}
}
}

function getDetails(itemName) {
request = createRequest();
if (request === null) {
alert("Unable to create request");
return;
}
var url= "getDetailsXML.php?ImageID=" + escape(itemName);
request.open("GET", url, true);
request.onreadystatechange = displayDetails;
request.send(null);
}

function displayDetails() {
if (request.readyState == 4) {
if (request.status == 200){
var detailDiv = document.getElementById("description");
for(var i = detailDiv.childNodes.length; i > 0; i--) {
detailDiv.removeChild(detailDiv.childNodes[i-1]);
}
var responseDoc = request.responseXML;
var description = responseDoc.getElementsByTagName("description")[0];
var descriptionText = description.firstChild.nodeValue;
var descriptionP = document.createElement("p");
descriptionP.appendChild(document.createTextNode("Description: " + descriptionText));
detailDiv.appendChild(descriptionP);
var price = responseDoc.getElementsByTagName("price")[0];
var priceText = price.firstChild.nodeValue;
var priceP = document.createElement("p");
priceP.appendChild(document.createTextNode("Price: $" + priceText));
detailDiv.appendChild(priceP);
var urlP = document.createElement("p");
var list = document.createElement("ul");
var urlElements = responseDoc.getElementsByTagName("url");
for(var i = 0; i < urlElements.length; i++){
var url = urlElements[i].firstChild.nodeValue;
var li = document.createElement("li");
var a = document.createElement("a");
a.setAttribute("href", url);
a.appendChild(document.createTextNode(url));
li.appendChild(a);
list.appendChild(li);
}
urlP.appendChild(list);
detailDiv.appendChild(urlP);
}
}
}
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Mar 29 2015 09:16am
Quote (CondescendenceX @ Mar 28 2015 10:10am)
Sorry if I seem ignorant but we just jumped into doing this and I am completely lost. This is the code we used in the lab, this is what I have to go off of.


Perhaps take a class like home ec instead
Member
Posts: 29,723
Joined: Jun 11 2007
Gold: 279.52
Mar 29 2015 02:26pm
Example Javascript:

Code
v<html>
<head>
</head>
<body>


<div id="daysotw">

</div>

<script>



var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "weeks.xml", false);
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send("");
xmlDoc = xmlhttp.responseXML;

var msgs = xmlDoc.getElementsByTagName("messages")[0];

var days = xmlDoc.getElementsByTagName("daily");


for(var i = 0; i < days.length; i++){
document.getElementById("daysotw").innerHTML += '<br/>'+ days[i].childNodes[0].nodeValue;
}

</script>



</body>
</html>



Example XML

Code
<?xml version="1.0"?>
<messages>
<daily>Today is Sunday.</daily>
<daily>Today is Monday.</daily>
<daily>Today is Tuesday.</daily>
<daily>Today is Wednesday.</daily>
<daily>Today is Thursday.</daily>
<daily>Today is Friday.</daily>
<daily>Today is Saturday.</daily>
</messages>


This post was edited by AkuuZ on Mar 29 2015 02:27pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll