Returning XML node values - javascript

I need to return all XML values from a URL.
I have previously used the URL and it works fine.
This is what I have so far:
function displayXML(xml) {
var devices = xml.getElementsByTagName("device");
for (var i = 0; i < devices.length; i++) {
var deviceDetails = devices[i].children;
for (j = 0; j < deviceDetails.length; j++) {
console.log(devices[i].childNodes[j].nodeValue);
}
}
}
It manages to return the right amount of values: 33 tags 33 values
but it's returning null for each one. However, the XML file contains values for each tag.
Thanks

Based on an answer to this question
The nodeValue property of XML elements is always null. The value of the element is actually stored within text nodes inside the element so you will need to go down one more child to get it. Try this
var devices = xml.getElementsByTagName("device")[i].firstChild.nodeValue;
I think your script should look something like this with firstChild inserted when trying to get the value:
function displayXML(xml) {
var devices = xml.getElementsByTagName("device");
for (var i = 0; i < devices.length; i++) {
var deviceDetails = devices[i].children;
for (j = 0; j < deviceDetails.length; j++) {
console.log(devices[i].childNodes[j].firstChild.nodeValue);
}
}
}

Related

How to hold all the data bound, not just the data of the visible page

First, I'm sorry I do not have enough English Level.
My Grid shows 20 rows on a page. To use Excel export with client template, I used the following source found in the forum.
function excelExportWithTemplates(e) {
var sheet = e.workbook.sheets[0];
var colTemplates = [];
var data = this.dataSource.view();
for (var i = 0; i < this.columns.length; i++) {
if (this.columns[i].template) {
colTemplates.push(kendo.template(this.columns[i].template));
} else {
colTemplates.push(null);
}
}
for (var i = 0; i < colTemplates.length; i++) {
for (var j = 0; j < data.length; j++) {
if (colTemplates[i] != null) {
sheet.rows[j + 1].cells[i].value = colTemplates[i](data[j]);
}
}
}
}
For example, if I have a total of 100 data, only 20 data, the size of one view,
The remainder can not be applied.
it doesn't mean
ExcelExport don't work well, I mean ExcelExport with ClientTemplate do work just only 20rows. (my view page amount)
To do this, add data.Source.View
I tried changing it to total
Total is just counting the number,
No conversion has been made.
To convert all data
What should I turn .view into?
The view() method will return only the rendered data on the viewport. Use the data() method instead, which will return all dataSource's data:
var data = this.dataSource.data();

Google Script: Clear content of cells with specific background (to slow)

I am new to Google Script and i got stuck.
The code below works perfectly, but it takes more than 10 min to loop through all named ranges.
It clears the content of cells, but not the one with green backgrounds.
function deletNoGreenCells() {
var namedRange = ['HaM','BeM','LoM']
for (var k=0; k<namedRange.length; ++k) {
var range=SpreadsheetApp.getActiveSpreadsheet().getRangeByName(namedRange[k])
for( var i = 1; i < range.getNumRows()+1; ++i){
for(var j = 1; j<range.getNumColumns()+1;++j){
if (range.getCell(i, j).getBackground()!= "#93c47d") {
range.getCell(i, j).clearContent()
}}}}}
How can i get this faster?
Cheers!
Thank You Matthew for the link to call getBackgrounds, wich leeds me to this solution:
function deletNoGreenCells() {
var namedRange = ['HaM','BeM','LoM']
for (var k = 0; k < namedRange.length; ++k) {
var range =SpreadsheetApp.getActiveSpreadsheet().getRangeByName(namedRange[k])
var backgrounds = range.getBackgrounds();
for(var i = 0; i < range.getNumRows(); ++i) {
for(var j = 0; j<range.getNumColumns(); ++j) {
if (backgrounds[i][j] != "#93c47d") {
range.getCell(i+1, j+1).clearContent()
}
}
}
}
}
Now it runs only 5 seconds. Thanks!
I haven't tried this, but if you're just clearing the data from the non-green cells you might be able to do this:
Call getData on the range to get an array containing all the values
Call getBackgrounds on the range to get the corresponding array of background colours
Use the backgrounds array to update the data array, blanking out the elements you want to clear
Call setData on the range, passing back the modified array
It seems likely that reading and writing the data in big blocks like this will be quicker.

Javascript - Seperate all IPs on page and use them as strings

I'm trying to grab all IPs on a specific page and run a function on each of them; however I can only find (through research) how to grab all IP's on the page as a single string. Not very proficient in JS, as you may be able to tell.
Grab as single string:
var markup = document.getElementsByClassName('border_wrapper')[0].innerHTML;
alert(markup.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g).join("\n"))
I'm assuming I'll need a for loop to run them in a function. resolve(newstring);
The .join makes a string, remove it;
var markup = document.getElementsByClassName('border_wrapper')[0].innerHTML;
var list = markup.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g);
if (list != null) {
for (var i = 0; i < list.length; i++)
alert(list[i]);
}
Using a cache to prevent repeats;
var markup = document.getElementsByClassName('border_wrapper')[0].innerHTML;
var list = markup.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g);
if (list != null) {
var processed = {};
for (var i = 0; i < list.length; i++) {
if (!processed[list[i]])
{
processed[list[i]] = true;
alert(list[i]);
}
}
}

How to remove the unnecessary html after appending?

var split = $('.split-locations').text().split('\n');
for (var i = 0; i < split.length; i++) {
$('.split-locations').append("<pre>"+split[i]+"</pre>");
}
<div class="split-location">
Singapore
USA
Aussie
</div>
On profile, the user type with enter for each address, but the problem is when it is saved, the addresses would be displayed on the webpage as:
Singapore USA Aussie
<pre>Singapore</pre>
<pre>USA</pre>
<pre>Aussie</pre>
I am not sure why the text (Singapore USA Aussie) is still there. Only wanted to display:
<pre>Singapore</pre>
<pre>USA</pre>
<pre>Aussie</pre>
How to remove the first line before <pre> bunch? Or how to replace text with pre bunch?
Update
One more thing: how to print first array outside loop because need to display first one outside loop and then put the rest of array inside accordion?
Here the code is: for you to understand what I am trying to do. see the comment.
var location = $('.split-locations');
var split = $('.split-locations').text().split('\n');
location.empty();
//need to print first location
location.append('<div class="accordion-location">');
for (var i = 0; i < split.length; i++) {
//print remaining location after minus first location
location.append("<pre>"+split[i]+"</pre>");
}
location.append('</div>');
});
You need to empty the container element before appending the pre nodes. Try this.
var split = $('.split-locations').text().split('\n');
$('.split-locations').empty();
for (var i = 0; i < split.length; i++) {
$('.split-locations').append("<pre>"+split[i]+"</pre>");
}
You need to clear out the location:
var split = $('.split-locations').text().split('\n');
$('.split-locations').empty();
for (var i = 0; i < split.length; i++) {
$('.split-locations').append("<pre>"+split[i]+"</pre>");
}
Update:
var $el = $('<div class="accordion-location"></div>');
for (var i = 0; i < split.length; i++) {
$el.append("<pre>"+split[i]+"</pre>");
}
location.append($el);

Looping through and updating the values in a select list

I'm trying to update the options of ALL select lists on a page and implemented a solution found on Get list of all `input` objects using JavaScript, without accessing a `form` object and other pages.
This works to an extent but only the select lists which occur AFTER the one which is triggering the javascript are updated whereas I need them ALL done, regardless of their position relative to the triggering select.
Here's a simplified version of what I have:
function chooseBon(id, value) {
var bonbonsAmount = 12;
var bonbonsCurrent = 0;
var bonbonsCount = 4;
var inputs, index;
// get all the select lists
inputs = document.getElementsByTagName('select');
// loop through all the lists
for (index = 0; index < inputs.length; ++index) {
// First disable all options
for (j = 0; j<=bonbonsAmount; ++j) {
inputs[index].options[j].disabled="disabled";
}
// Then re-enable the ones we still need
for (j = 0; j<=(bonbonsAmount - bonbonsCurrent); ++j) {
inputs[index].options[j].disabled="";
}
// add up the no of chocs selected so we know how many options to re-enabled above
bonbonsCurrent += inputs[index].selectedIndex;
}
I'm an admitted newbie and am adapting a script from one ecommerce platform for another so am hamstrung in certain areas so feel free to make other suggestions.
here is one of possible solutions and the fiddle:
function chooseBon() {
var bonbonsAmount = 12;
var bonbonsCurrent = 0;
var bonbonsRemaining; //max. is bonbonsAmount
var inputs, i, j;
inputs = document.getElementsByTagName('select');
for (i = 0; i < inputs.length; i++) {
bonbonsCurrent += parseInt(inputs[i].value, 10);
}
bonbonsRemaining = bonbonsAmount - bonbonsCurrent;
for (i = 0; i < inputs.length; i++) {
for (j = 0; j <= bonbonsAmount; j++) {
inputs[i].options[j].disabled = (j < bonbonsRemaining + parseInt(inputs[i].value, 10) + 1) ? false : true;
}
}
}

Categories

Resources