I am trying to read a XML file with Javascript , it is for a assignment in school so i cant use jQuery it has to be Javascript. Basicly i can read ONE value but not the other
This is my JS that is resposible for reading the XML value. It will read startLng but it gives me startLat is undefined. But if i check the XML file the startLat is not undefined. I cant see what the problem is here.
What i am trying to do is to get the LatitudeDegrees and LongitudeDegrees from the XML file. But it only gives me the LongitudeDegrees and says the LatitudeDegrees is undefined. What am i doing wrong here?
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
getLatLang(xmlhttp);
}
};
xmlhttp.open("GET", "G1.TCX", true);
xmlhttp.send();
}
function getLatLang(xml)
{
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("Trackpoint");
startLat = x[0].getElementsByTagName("Position")[0].getElementsByTagName("LatitudeDegrees")[0].childNodes[0].nodeValue;
startLng = x[0].getElementsByTagName("Position")[0].getElementsByTagName("LongitudeDegrees")[0].childNodes[0].nodeValue;
}
This is the XML file
<Trackpoint>
<Time>2008-10-28T15:58:22Z</Time>
<Position>
<LatitudeDegrees>59.4111992</LatitudeDegrees>
<LongitudeDegrees>13.5304104</LongitudeDegrees>
</Position>
<AltitudeMeters>85.6945801</AltitudeMeters>
<DistanceMeters>0.2149343</DistanceMeters>
<HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
<Value>116</Value>
</HeartRateBpm>
<SensorState>Absent</SensorState>
<Extensions>
<TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2" CadenceSensor="Footpod"/>
</Extensions>
</Trackpoint>
This has been solved.
The problem was i did not have a document load.
Related
I am finding a way to load multiple files at a time in my html document using vanilla AJAX ( I don't want any dependecy, the way I will get using jQuery ). Here is a piece of code I grabbed from W3schools' AJAX Documentation. :
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.querySelector("#tst").innerHTML = this.responseText;
}
}
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
I am pretty new to AJAX. So, I have a confusion. The above function works fine, but I have multiple files to load and I don't want to call this function multiple times, to load the files. Let me describe my desired output. I am imagining the syntax of the updated function will be :
loadDoc(["one.txt", "two.txt", "three.txt"])
So, we observe that the input will be an array. And now the ouput will follow this format :
content of "one.txt" + break + content of "two.txt" + break + content of "three.txt" and so on....
If the content of "one.txt" is "Hello World !", content of "two.txt" is "I love", and content of "three.txt" is "Javascript", the output will be :
Hello World !
I love
Javascript
So, can you please help me update my code ?
Pass the list of files to load to a PHP/ASP/whatever page which assembles the text for you.
function loadDoc(files) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.querySelector("#tst").innerHTML = this.responseText;
}
}
xhttp.open("GET", "ajax_info.php?files="+files, true);
xhttp.send();
}
The files parameter could be a comma delimited list, which the page uses to get the contents and assemble it into a single return string.
I am new to using ajax. Now I have a project to create a localhost chatbox web page. And I want to save XML document using javascript. Here is my XML file.
<root>
<msg>Mesage 1</msg>
<msg>Message 2</msg>
</root>
Here is my javascript Code.
var xhttp = new XMLHttpRequest();
var doc;
xhttp.open("POST","det.xml");
xhttp.send(null);
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
doc = xhttp.responseXML;
var elem = doc.createElement("msg");
elem.textContent = "Hello XML";
doc.documentElement.appendChild(elem);
}
};
But after run this web XML document has no changed. Please tell me how to save xml using ajax. Thanks in advance. And sorry for my poor english.
I am trying to create an application that will show, and periodically change, a paragraph of text (like a news article or similar).
I want the data to come from an xml file so other people can add stories/remove old stories etc.
I'm trying to get my JavaScript to populate a single html field with data from an xml file. Then after a given time, for now we'll say 4 seconds, it will change to the next piece of data.
Below is a very crude version of what I've been trying to do:
HMTL:
<head>
<script src="script.js"></script>
</head>
<body>
<div id="text"></div>
</body>
XML:
<document>
<text>one</text>
<text>two</text>
<text>three</text>
</document>
JavaScript:
var timer = setInterval(addText,4000);
var xhttp = new XMLHttpRequest();
xhttp.onreadystaechange = function() {
if(this.readyState == 4 && this.status == 200) {
addText(this);
}
};
function addText(xml) {
var xmlDoc = xml.responseXML;
var count = 0;
var max = xmlDoc.GetElementsByTagName("text");
document.getElementById("text").innerHTML =
xmlDoc.getElementByTagName("text")[count].childNodes[0].nodeValue;
if (count < max.length) {
count++;
} else {
count = 0;
}
}
xhttp.open("GET", "XMLFile.xml",true);
xhttp.send();
The current problem I am experiencing is that the first xml field populates successfully, but then I get an error saying "Unable to get property 'responseXML' of undefined or null reference".
Ideally what I'd also like is for the xml document to be opened everytime the function occurs, so the application doesn't have to be restarted if extra data is added to the xml file - if that makes sense (and is possible)
You can place addText in the scope of onreadystatechange handler. Something like this.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
var xmlDoc = this.responseXML;
var count = 0;
var xText = xmlDoc.getElementsByTagName("text");
var max = xText.length;
var docText = document.getElementById("text");
function addText() {
//docText, xText and count are available from parent scope
docText.innerHTML =
xText[(count++) % max].childNodes[0].nodeValue;
}
var timer = setInterval(addText,4000);
}
};
xhttp.open("GET", "XMLFile.xml",true);
xhttp.send();
i was trying to write a javascript program which read list of token from the configuration file and set the token. But javascript program failed to read my local configuration file.
javascript program below
require([
"splunkjs/mvc",
"splunkjs/mvc/simplexml/ready!"
],
function(mvc) {
var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
alert("start");
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.status == 200) {
var allText = xhttp.responseText;
alert('Text = ' + allText)
}
};
xhttp.open("GET", "rejected_panels.conf", true);
xhttp.send();
alert("end");
defaultTokenModel.set('home_panel_1', 'yes');
});
I am accessing javascript from splunk's simple xml as below
<form script="rejected_panels.js">
....
....
</form>
I got this error in chrome browser
rejected_panels.js:21 GET http://localhost:8000/en-GB/app/multi_tOP/rejected_panels.conf 404
Kindly Help me on this.
I am trying to get some information from the Yahoo Console for weather conditions. The xml result tree from Yahoo looks like this: result tree
I am able to get things that are only one level deep with the code I have. My variable 'beta' is for the title which works beautifully. The second variable is 'alpha' which will not work. I believe it has to do with how I call for it 'item.condition.temp' and with the node values.
I am very new to this so please list any sources you use as it will help me going forward.
<!DOCTYPE html>
<html>
<head>
<p id="alpha"></p>
<p id="beta"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
myFunction(xhttp);
}
};
xhttp.open('GET', 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D2444827&diagnostics=true', true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById('beta').innerHTML =
xmlDoc.getElementsByTagName('title')[0].childNodes[0].nodeValue;
document.getElementById('alpha').innerHTML =
xmlDoc.getElementsByTagName('item.condition.temp')[0].childNodes[0].nodeValue;
}
</script>
I would suggest adding &format=json to your URL to make it this:
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D2444827&diagnostics=true&format=json
Then parse the resulting JSON which is a more JavaScript way of doing it.