I'm trying to parse this XML, and I can't extract all of the "" texts in that file, can someone please tell me what I'm doing wrong?
Here is the HTTP /GET that I'm trying to make a request to,
request_link:"https://cdnsecakmi.kaltura.com/api_v3/index.php/service/caption_captionAsset/action/serve/captionAssetId/0_o7nr2wfk/v/2/ks/djJ8MjAxMTUzMXyLXeiobMjq3pQ7i9wYSlwZHhVUdAP8C6IYMqJGpcDqOsBc4X5e0rTARtFc6ysPSkDTq_u7qfycIGeJquoL_O_3MlBxOn9rBGgQBZXeeAQrcd8tmZqjZo2h5bQVGtQXgFHVc7OyICn-cY2lHa4ZjRp6w2ueyIRqyNp9pHZ5onX2Z6gfjyNR4MubV3V6Qwfa3e0hbRdAoCap1dgO2QIawMKi/.dfxp"
const get_religion_notes = new XMLHttpRequest();
get_religion_notes.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
console.log(get_religion_notes.responseXML)
let parser = new DOMParser();
xmlDoc = parser.parseFromString(get_religion_notes,"text/xml");
console.log(document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("p")[0])
document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("p")[0]; //Undefined why is that?
}
};
get_religion_notes.open("GET", "https://cdnsecakmi.kaltura.com/api_v3/index.php/service/caption_captionAsset/action/serve/captionAssetId/0_o7nr2wfk/v/2/ks/djJ8MjAxMTUzMXyLXeiobMjq3pQ7i9wYSlwZHhVUdAP8C6IYMqJGpcDqOsBc4X5e0rTARtFc6ysPSkDTq_u7qfycIGeJquoL_O_3MlBxOn9rBGgQBZXeeAQrcd8tmZqjZo2h5bQVGtQXgFHVc7OyICn-cY2lHa4ZjRp6w2ueyIRqyNp9pHZ5onX2Z6gfjyNR4MubV3V6Qwfa3e0hbRdAoCap1dgO2QIawMKi/.dfxp", true);
get_religion_notes.send();
Everything is correct.
just make sure your using responseText parser requires text xml not xml itself..
const get_religion_notes = new XMLHttpRequest();
get_religion_notes.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
console.log(get_religion_notes.responseText)
let parser = new DOMParser();
xmlDoc = parser.parseFromString(get_religion_notes.responseText,"text/xml");
console.log(document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("p")[0])
document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("p")[0]; //Undefined why is that?
}
};
get_religion_notes.open("GET", "https://cdnsecakmi.kaltura.com/api_v3/index.php/service/caption_captionAsset/action/serve/captionAssetId/0_o7nr2wfk/v/2/ks/djJ8MjAxMTUzMXyLXeiobMjq3pQ7i9wYSlwZHhVUdAP8C6IYMqJGpcDqOsBc4X5e0rTARtFc6ysPSkDTq_u7qfycIGeJquoL_O_3MlBxOn9rBGgQBZXeeAQrcd8tmZqjZo2h5bQVGtQXgFHVc7OyICn-cY2lHa4ZjRp6w2ueyIRqyNp9pHZ5onX2Z6gfjyNR4MubV3V6Qwfa3e0hbRdAoCap1dgO2QIawMKi/.dfxp", true);
get_religion_notes.send();
Related
I'm currently doing some practice and i want to print the titles of each movie on this api:
https://jsonmock.hackerrank.com/api/movies?Year=1998
Basically, I want each title to be printed for the first page (or preferably a specific page).
This is the code I have:
<script>
function printTItles(year) {
var res;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
res = JSON.parse(this.responseText);
for(var i=0;i<res.per_page;i++){
document.getElementById("demo").innerHTML = res.data.i.Title;
}
};
}
xmlhttp.open("GET", "https://jsonmock.hackerrank.com/api/movies?Year=<year>", true);
xmlhttp.send();
}
</script>
I know the problem is in res.data.i.title but I'm not sure how to fix it.
You are trying to access the element at the index i in your loop, like you would access a property of an object. To get the element at position i in your res.data array, you need the square bracket access [ ]
Also, you are not replacing the year parameters in your request for the year parameters passed to your function. You might want to check that out.
Here I have use the year 2018 as an example.
function printTItles(year) {
var res;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
res = JSON.parse(this.responseText);
for(var i=0;i<res.per_page;i++){
document.getElementById("demo").innerHTML = res.data[i].Title;
}
};
}
xmlhttp.open("GET", "https://jsonmock.hackerrank.com/api/movies?Year=2018", true);
xmlhttp.send();
}
printTItles();
<div id="demo"></div>
You could add some more improvement. For example, at each iteration, you are replacing the content of your #demo element. This cause only the last title to be shown. You could, instead, append the data to the already existing html of the div. Or, like I did in this case, build your string before setting it as the new innerHTML value.
function printTItles(year) {
var res;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
res = JSON.parse(this.responseText);
var output = "";
for(var i=0;i<res.per_page;i++){
if(res.data[i]) {
output += res.data[i].Title + '<br />';
}
}
document.getElementById("demo").innerHTML = output;
};
}
xmlhttp.open("GET", "https://jsonmock.hackerrank.com/api/movies?Year=2018", true);
xmlhttp.send();
}
printTItles();
<div id="demo"></div>
I've also added a condition to check if there is an element at res.data[i].
im currently working on reading a xml file, i have successfully read the xml file so far but the problem comes next, instead of reading the xml file tag one by one i want to read the tags that are inside the items tag and extract the value of each one
So far this is what i got
var xmlDoc;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'prueba.xml');
xhr.send();
xhr.onreadystatechange = function(){
xmlDoc = xhr.responseXML;
var items = xmlDoc.getElementsByTagName("items")[0];
for(i=0;i<items.length;i++){
item = items[i];
$(item).each(function(i,val){
console.log(val)
})
}
}
and here's the xml file
<!DOCTYPE StreamControl>
<items>
<timestamp>1510794812</timestamp>
<nombre>ayy</nombre>
</items>
I think i'm close, any tips?
try this:use node.firstChild.nodeType === 3 && node.firstChild.textContent.trim() !== '' to filter the text;
var xmlDoc;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'prueba.xml');
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
xmlDoc = xhr.responseXML;
var items = xmlDoc.getElementsByTagName("*");
let res = {};
Array.from(items).forEach(node => {
if (node.firstChild.nodeType === 3 && node.firstChild.textContent.trim() !== '') {
res[node.nodeName] = node.firstChild.textContent
}
});
console.log(res);//{timestamp: "1510794812", nombre: "ayy"}
}
}
I am working with JIVE JSON RESTFul API and on their API they have a security
(I am referring to the throw 'allowIllegalResourceCall is false.'; )
throw 'allowIllegalResourceCall is false.';
{
"id" : "52104",
}
I am using this code to try to PARSE IT:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("topdisplay").innerHTML = myObj.id;
}
};
xmlhttp.open("GET", "http://mysite/API/",true);
xmlhttp.send();
And I am getting an error because of that starting line.
I looked everywhere to try finding a solution to skip that to the JSON PARSE would work but I can't seem to find a way that works to do it.
Also I know that the parsing code works because when I remove the first line it works perfectly.
Any help?
JIVE REST API introduced this to help prevent against JSON Hijacking back when web browsers were susceptible.
You'll need to first find {, and do a substring from that position to the end of the string.
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = this.responseText
var myObj = JSON.parse(this.responseText.substring(this.response.indexOf('{')));
document.getElementById("topdisplay").innerHTML = myObj.id;
}
};
xmlhttp.open("GET", "http://mysite/API/",true);
xmlhttp.send();
NOTE:
using RegEx, you'll need to find the first line with throw and ending in a semi-colon, if found replace with an empty string.
JSON.parse(response.replace(/throw.*;/, "").trim());
I have defined this XML file
<?xml version="1.0" encoding="UTF-8"?>
<root>
<town>
<area>20000</area>
<population>10000</population>
</town>
</root>
with AJAX I load this and I try to parse it.
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
myFunctionParsing(this);
}};
xhttp.open("GET", "towns.xml", false);
xhttp.send();
function myFunctionParsing(xml)
{
var xmlDoc = xml.responseXML;
var nodes = xmlDoc.getElementsByTagName("town");
var n = nodes[0].childNodes[0];
console.log(n.tagName);
var aDiv = document.getElementById("area");
aDiv.innerHTML = n.nodeValue;
}
With that script I want to write in div value of node "area" in some div with name "area".
Why can not I write this value? Why n.tagName is undefined?
A couple things:
I am using MDN's XMLHttpRequest example, which shows how to override the mime type to force it to parse the response as XML. That way you don't have to do responseXML.
querySelector is great! Use it. No need for getElementsByTagName.
function parseXML(xml) {
var area = xml.querySelector('town area');
console.log(area.textContent); // => 20000
}
var url = 'https://gist.githubusercontent.com/gabrielflorit/c618f26ac367fbaf91846efe73913c23/raw/b3c9c3e8d0f1dc747006103442b3b19bf7c91d9c/town.xml';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
// If specified, responseType must be empty string or "document"
xhr.responseType = 'document';
// overrideMimeType() can be used to force the response to be parsed as XML
xhr.overrideMimeType('text/xml');
xhr.onload = function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
parseXML(xhr.response);
}
}
};
xhr.send(null);
I'm having a lot of difficulty with this project.
My aim is to write the results of a HTML form to an XML Document using Javascript.I have absolutely no idea how to do it.
Reason why I'm coming here is that I want to be sure that I'm on the right track. So far, I'm writing only one line "\n" just to test things out.
Here is my current JavaScript
var xhr = false;
if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
function StoreRegXml()
{
xhr.open("GET", "php.php?" + Number(new Date), true);
xhr.onreadystatechange = getData;
xhr.send(null);
}
function getData()
{
if ((xhr.readyState == 4) && (xhr.status == 200))
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filename = "customer.xml";
var file = fso.CreateTextFile(filename, true);
file.WriteLine('<?xml version="1.0" encoding="utf-8"?>\n');
file.Close();
}
}
Am I on the right track?
Edit: I'm adding alerts('test1'); to see where the code is going wrong and it stops at
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
Any ideas?
Inside the browser to create and populate an XML DOM document you can use the W3C DOM APIs with e.g.
var xmlDoc = document.implementation.createDocument(null, 'root', null);
var foo = xmlDoc.createElement('foo');
foo.textContent = 'bar';
xmlDoc.documentElement.appendChild(foo);
console.log(xmlDoc);
This creates an in memory XML DOM document, not an XML file. You can then for instance send the xmlDoc with XMLHttpRequest to the server.